Reputation: 8836
How can I use a private parent class as a parent of an internal class?
I'm try to do this:
class A
{
};
class B : private A
{
};
class C : private B
{
public:
class D : public A
{
};
};
int main()
{
C c;
}
But I'm getting the following error. Is there any way to work around it, or do I need to change the private to protected?
test.cpp:14:20: error: 'A' is a private member of 'A'
class D : public A
^
test.cpp:6:11: note: constrained by private inheritance here
class B : private A
^~~~~~~~~
test.cpp: 1: 7: note: member is declared here
class A
^
1 error generated.
Upvotes: 1
Views: 82
Reputation: 29022
The compiler thinks you are trying to refer to C's parent's parent's type. Specify the type of A
completely to avoid this ambiguity. Use ::
to denote the global namespace.
class C : private B
{
public:
class D : public ::A
// Add this ^^
{
};
};
This is a case of Injected class name.
For the name of a class or class template used within the definition of that class or template or derived from one, unqualified name lookup finds the class that's being defined as if the name was introduced by a member declaration (with public member access)
Upvotes: 5