The following question brought this phenomenon to notice where the constructor is being called even in the private mode of inheritance.
I tried it on the Diamond Problem, then made it simpler by breaking the diamond rule and just keeping virtual inheritance.
Further followed this up by a simple example of a 3-level inheritance which I am showing below (C
inherited by B
and B
inherited by C
- both in private inheritance) and still the constructor of A
is being called.
Shouldn't A()
be private
and inaccessible in C
?
#include<iostream>
using namespace std;
class A
{
public:
A(){ cout << "1"; }
};
class B: A
{
public:
B(){ cout << "2"; }
};
class C: B
{
public:
C(){ cout << "3"; }
};
int main()
{
C c1;
}
Output: 123
(You can also view the code and output here)
P.S: I have tried it both with the normal case (given here) and with virtual
inheritance, even with the "Diamond-Problem" - the answer is same every time.