我正在经历钻石问题,并认为可以在各种情况下工作。这是我正在研究的其中之一。
#include <iostream>
using namespace std;
class MainBase{
public:
int mainbase;
MainBase(int i):mainbase(i){}
void geta()
{
cout<<"mainbase"<<mainbase<<endl;
}
};
class Derived1: public MainBase{
public:
int derived1;
int mainbase;
Derived1(int i):MainBase(i),derived1(i) {mainbase = 1;}
public:
void getderived1()
{
cout<<"derived1"<<derived1<<endl;
}
};
class Derived2: public MainBase{
public:
int derived2;
int mainbase;
Derived2(int i):MainBase(i),derived2(i){mainbase = 2;}
public:
void getderived2()
{
cout<<"derived2"<<derived2<<endl;
}
};
class Diamond: public Derived1, public Derived2{
public:
int diamond;
int mainbase;
Diamond(int i,int j, int x):Derived1(j),Derived2(x),diamond(i){mainbase=3;}
public:
void getdiamond()
{
cout<<"diamond"<<diamond<<endl;
}
};
int main()
{
Diamond d(4,5,6);
// cout<< d.MainBase::mainbase;
cout<<"tested"<<endl;
cout<<d.mainbase;
cout<<d.Derived2::mainbase<<endl;
cout<<d.Derived1::mainbase<<endl;
/*cout<<d.Derived2::MainBase::mainbase<<endl;
cout<<d.Derived1::MainBase::mainbase<<endl;*/
}
我现在想知道如何访问 MainBase 类 mainbase 变量?任何输入。