sukumar
sukumar

Reputation: 129

Problem in virtual inheritance

class base { 
protected: 
    base() {} 
}; 

class der1 :  virtual private base { 

public: 
    der1() {} 
}; 

class der2 : public der1
{
public:
    der2() {}

};

int main() {
    der2 d;
}

It gives compile time error: 'base::base' : cannot access inaccessible member declared in class 'base'

But base class constructor is define publically it compiles.

Pls anyone can give explaination?

Upvotes: 0

Views: 214

Answers (3)

user934691
user934691

Reputation:

Replace virtual private base with virtual protected base and der2 will be able to access to the constructor of base

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 791929

Because base is a virtual base class, it must be initialized by the most derived class in the hierarchy of an object being instantiated. base's contructor may be protected and accessible to classes derived from it, but that doesn't help as base is a private base class of der1 so even classes derived from der1 don't have access to the base parts of "*this".

You need to relax the access restrictions on the base base class to at least protected.

Upvotes: 2

arne
arne

Reputation: 4674

The base class constructor is declared protected, but that is not the problem. The main problem will be the private inheritance in der1. This way, der2 cannot access the constructor of base which it needs in order to construct itself.

Upvotes: 0

Related Questions