chuck1
chuck1

Reputation: 675

Multiple inheritance makes private member accessible

class A {
    public:
            int a;
};

class B: private A {
};

class C: public A {
};

class D: public B, public C {
        D() {
                B::a = 0;
        }
};

This compiles even though B privately inherits A. If I remove D's inheritance of C, the compiler says a is inaccessible, like I'd expect. So is the inheritance of C confusing my compiler?

Compiler is gcc 4.4.7

Upvotes: 10

Views: 210

Answers (1)

Deduplicator
Deduplicator

Reputation: 45654

Looks like a genuine compiler bug, as the standard does not allow such access in

11.2 Accessibility of base classes and base class members

Looking for evidence outside the standard itself, WhozCraig already brought up that clang does not allow such access.

Looking for similar patterns which might be confused in gcc, there is diamon-inheritance with virtual base class A, which would have allowed such access, as the path of most access determines what protections apply.

Upvotes: 4

Related Questions