以下は、多重継承で直面するひし形の問題です。
class Base {
public:
Base() {
cout << "Empty Base constructor " << endl;
}
Base(const string & strVar) {
m_strVar = strVar;
cout << m_strVar << endl;
}
virtual ~Base() {
cout << "Empty Base destructor " << endl;
}
virtual const string & strVar() const {
return m_strVar;
}
string m_strVar;
};
class Derived1: public virtual Base {
public:
Derived1() {
cout << "Empty Derived1 constructor " << endl;
}
Derived1(const string & strVar) : Base(strVar) {
cout << " Derived1 one arg constructor" << endl;
}
~Derived1() {
cout << "Empty Derived1 destructor " << endl;
}
};
class Derived2: public virtual Base {
public:
Derived2() {
cout << "Empty Derived2 constructor " << endl;
}
Derived2(const string & strVar) : Base(strVar) {
cout << "Derived2 one arg constructor" << endl;
}
~Derived2() {
cout << "Empty Derived2 destructor " << endl;
}
};
class Derived: public Derived1, public Derived2 {
public:
Derived(const string & strVar) : Derived1(strVar), Derived2(strVar) {
cout << "Derived Constructor " << endl;
}
~Derived() {
cout << "Empty Derived destructor " << endl;
}
};
int main() {
Derived derObj ("Print this if you can ! ");
}
私が得る出力は
- 空のベース コンストラクター
- Derived2 1 つの引数コンストラクター
- Derived1 1 つの引数コンストラクター
- 派生コンストラクター
- 空の派生デストラクタ
- 空の Derived2 デストラクタ
- 空の Derived1 デストラクタ
- 空のベース デストラクタ
私のderObjパラメータ、つまり「可能であればこれを印刷してください」が印刷されず、出力が似ていないのはなぜだろうか
- 空のベース コンストラクター
- Derived2 1 つの引数コンストラクター
- できればこれを印刷してください!
- Derived1 1 つの引数コンストラクター
- 派生コンストラクター
- 空の派生デストラクタ
- 空の Derived2 デストラクタ
- 空の Derived1 デストラクタ
- 空のベース デストラクタ