Original Post
#include <iostream>
class base {
};
class derived : base {
};
class final_derived : public derived {
base *m_parent; // this line generates error c2247
public:
};
int main(int argc, const char *argv[])
{
printf("wtf\n");
return 0;
}
This code does not compile, giving me this error:
error C2247: 'base' not accessible because 'derived' uses 'private' to inherit from 'base'
My understanding of private inheritance is simply that only the derived class gets to treat itself as an instance of the privately inherited base class. In the case I have presented above, that would mean "final_derived" does not get to know that "derived" is also a "base," and it does not get to access any of "base's" members.
I do not however understand why "final_derived" is not allowed to have ANY REFERENCE to the type which "derived" is inherited from ("base"). I'm not trying to access anything, I just have a pointer! I could understand if I was like "m_parent = this" because I would be trying to access a conversion which isn't available to me, but I'm not doing that. I just want a reference to a "base". I don't see what access rules I'm violating!
EDIT: After googling my error code I've discovered that if I define m_parent as an instance of "::base *" instead of "base *" I don't get the error. This leads me to believe that when I declare an instance of a certain type, I must first look in my class namespaces for that type, then up through my derived classes, etc. In my case I must be finding the type "base" in the namespace of the class "derived", but it is a private type, so "final_derived" does not have access to it, and I must explicitly declare my pointer as a "::base *" to avoid the namespace confusion. Is this correct?
If this is what's happening, then it's weird that base classes are treated as member types in the classes that derive from them.