Accessing the name of a private-inherited class from a subclass

This is a great bug!

In the context of C, “A” means “the injected-class-name A that I get from my base”.
You could think of it as C::B::A.
Of course, that base is inaccessible due to private inheritance:

class A {};

class B : private A {
public:
   B() {}
   B(const A&) {}  
};

class C : public B {
public:
   C(const A&) {}
};

int main()
{
   A a;
   B b(a);
   C c(a);
}

// main.cpp:11:13: error: 'class A A::A' is inaccessible within this context
//      C(const A&) {}
//              ^
// main.cpp:1:9: note: declared here
//  class A {};
//        ^

(live demo)

You can fix this by calling it ::A, taking a more indirect route to actually name the exact same type:

class A {};

class B : private A {
public:
   B() {}
   B(const A&) {}  
};

class C : public B {
public:
   C(const ::A&) {}
};

int main()
{
   A a;
   B b(a);
   C c(a);
}

// OK

(live demo)


As an aside, the exact same logic applies to privately inherited member variables:

int x = 1;

class A
{
private:
   int x = 2;
};

class B : A {
public:
   B() { int y = x; }
};

int main()
{
   A a;
   B b(a);
}

// main.cpp: In constructor 'B::B()':
// main.cpp:11:17: error: 'int A::x' is private within this context
//    B() { int y = x; }

(live demo)

It all does seem pretty stupid on the face of it, but there’s probably a good reason.


In your case, then:

Container2(const ::QByteArray &ba);

Leave a Comment