Consider the below code:
#include
using namespace std;
class A
{
public:
A() {cout << \"1\";}
A(const A &obj) {cout <<
The way you have inherited your classes, all of them use private
inheritance.
By changing the inheritance of B
from A
and C
from A
to be protected
or public
, you can resolve the problem.
class B : protected virtual A
{
...
}
class C : protected virtual A
{
...
}
or
class B : public virtual A
{
...
}
class C : public virtual A
{
...
}
and then update D
's copy constructor to:
D(const D & obj) : A(obj), B(obj), C(obj) {cout <<"8";}
PS It's baffling to me that the default constructor works even with private
inheritance.