1
votes

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.

2
If A is privately owned by B and C, D cannot access it. It's that simple.Patrick Roberts
A's constructor is inaccessible because D has not a subclass of type A, instead it derives from B and C that derive privately from A. The other way around works for you implicitly construct C and B and they implicitly construct A, that is something they are allowed to do being it a direct subclass.skypjack
Maybe the fix would be class D: virtual private A, private B, private CM.M
Or else just remove the A() call. It will happen anyway, but at the correct point, and this isn't it. You should only be calling constructors for direct base classes or instance members here.user207421
@PatrickRoberts, Then why "D() {cout << "4";}" works. How does it call A() constructor even in private inheritance?Gaurav

2 Answers

0
votes

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.

0
votes

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/