Consider the code below:
#include<iostream>
using namespace std;
class A
{
public:
A() {cout << "1";}
A(const A &obj) {cout << "2";}
};
class B: virtual A
{
public:
B() {cout << "3";}
B(const B & obj) {cout<< "4";}
};
class C: virtual A
{
public:
C() {cout << "5";}
C(const C & obj) {cout << "6";}
};
class D:B,C
{
public:
D() {cout << "7";}
D(const D & obj) {cout << "8";}
};
int main()
{
D d1;
cout << "\n";
D d(d1);
}
The output of the program is below:
1357
1358
So, for the string D d(d1), the class copy constructor Dis called. During inheritance, we need to explicitly call the constructor of the copy of the base class, otherwise only the default constructor of the base class will be called. I understood so far.
My problem:
Now I want to call the constructor of copying all base classes at runtime D d(d1). For this, if I try below,
D(const D & obj) : A(obj), B(obj), C(obj) {cout << "8";}
then I get this error: Error:'class A A::A' is inaccessible within this context
. , A, B C, D. , .