C++ private inheritance and static members/types
- by WearyMonkey
I am trying to stop a class from being able to convert its 'this' pointer into a pointer of one of its interfaces. I do this by using private inheritance via a middle proxy class. The problem is that I find private inheritance makes all public static members and types of the base class inaccessible to all classes under the inheriting class in the hierarchy.
class Base
{
public:
enum Enum
{
value
};
};
class Middle : private Base
{
};
class Child : public Middle
{
public:
void Method()
{
Base::Enum e = Base::value; // doesn't compile BAD!
Base* base = this; // doesn't compile GOOD!
}
};
I've tried this in both VS2008 (the required version) and VS2010, neither work.
Can anyone think of a workaround? Or a different approach to stopping the conversion?
Also I am curios of the behavior, is it just a side effect of the compiler implementation, or is it by design? If by design, then why? I always thought of private inheritance to mean that nobody knows Middle inherits from Base. However, the exhibited behavior implies private inheritance means a lot more than that, in-fact Child has less access to Base than any namespace not in the class hierarchy!