Gaurav
Gaurav

Reputation: 181

Why default constructor of most base class (Virtual) is not getting called in private virtual inheritance while creating object of most derived class?

How default constructor of most base class is getting called in private virtual inheritance while creating object of most derived class. But the same does not get called when mentioned in constructor initializer list of most derived class.

#include<iostream>
using namespace std;
class A
{
public:
     A() {cout << "1";}

};

class B: private virtual A
{
public:
    B() {cout << "2";}

};

class C: private virtual A
{
public:
   C() {cout << "3";}

};

class D: private B, private C
{
public:
    D() : A(), B(), C() {cout << "4";}
    //D()  {cout << "4";}

};

int main()
{
   D d1;
   cout << "\n";
}

My Problem:

For Below mentioned code

D() : A(), B(), C() {cout << "4";}

I get below compilation error:

error: 'class A A::A' is inaccessible within this context

Why A() constructor is inaccessible here?

On the other hand below mentioned code gets compiled successfully and A() constructor gets called.

D()  {cout << "4";}

Output of the program is: 1234 Means A() constructor is getting called.

So, Why is the change in behavior for calling of constructor A() in above two cases?

What I know:

(1) When I do 'Public Virtual inheritance' of B & C, then default constructor of most base class is gets called even if it's in mentioned in constructor initializer list of most derived class. Means below statement compiles. D() : A(), B(), C() {cout << "4";}

(2) In virtual Inheritance, constructor of virtual base class is called directly from most derived class's constructor.

It might be a concept issue for me of virtual inheritance. Kindly help me to understand this and share good references for that.

Upvotes: 1

Views: 454

Answers (2)

honey_badger
honey_badger

Reputation: 530

As was mentioned in the comments, A() cannot be explicitly called from D() because A() is a private member of B and C. private inheritance gives access to public and protected members and makes them private. Whatever was private, is not inherited. So the key point is access specifiers during inheritance, not virtual inheritance. You can try and remove virtual from your example. The program will print 12134 because virtual inheritance was providing a common base object.

In case someone wonders why 1234 (or 12134 in non-virtual case) is outputted by the program, I invite them to read about the order of construction of derived classes: https://www.learncpp.com/cpp-tutorial/order-of-construction-of-derived-classes/

Upvotes: 0

user207421
user207421

Reputation: 310916

A is private from the point of view of D, but even if it wasn't it is incorrect for D to try to construct A. It should only construct direct base classes and instance member objects. B and C will both see to the construction of A by default.

Upvotes: 0

Related Questions