Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Inaccessible direct base' caused by multiple inheritance

Tags:

Spoiler alert: Maybe a stupid question. :)

#include <iostream>  using namespace std;  class Base {     public:         virtual void YourMethod(int) const = 0; };  class Intermediate : private Base {     public:         virtual void YourMethod(int i) const         {             cout << "Calling from Intermediate" << i << "\n";         } };  class Derived : private Intermediate, public Base {     public:         void YourMethod(int i) const         {             cout << "Calling from Derived : " << i << "\n";         } };  int main() { } 

Can someone Explain to me why this throws the compiler warning:

main.cpp:21: warning: direct base ‘Base’ inaccessible in ‘Derived’ due to ambiguity 

Now, I understand that there is no way this code will work. I want to know why. Base is private to Intermediate so it should not be visible to Derived through Intermediate. So where does the ambiguity come from? In constructor?

like image 628
nakiya Avatar asked Nov 07 '10 15:11

nakiya


1 Answers

This has nothing to do with overriding functions. It has to do with conversions. It really doesn't have to do with accessibility (i.e "private" or such) directly either. Here is a simpler example

struct A { int a; }; struct B : A { }; struct C : B, A { }; // direct A can't be referred to! 

You can refer to the indirect A object by first converting to B and then to A:

B *b = &somec; A *a = b; 

You cannot do such with the direct A object. If you try to directly convert to A, it will have two possibilities. It follows that it is impossible to refer to the non-static data members of the direct A object given a Derived object.

Notice that accessibility is orthogonal to visibility. Something can be accessible even tho it's not visible (for example by refering to it by a qualified name), and something can be visible even though it's not accessible. Even if all the above derivations would be declared private, the problem would still show up: Access is checked last - it won't influence name lookup or conversion rules.

Also, anyone can cast to an unambiguous private base class with defined behavior (the C++ Standard makes an exception for this) using a C-style cast, even if normally access wouldn't be granted to do so. And then there are still friends and the class itself that could freely convert.

like image 106
Johannes Schaub - litb Avatar answered Oct 14 '22 21:10

Johannes Schaub - litb