直接用代码说明问题:
1#include <iostream> 2using namespace std; 3 4class A { 5public: 6 A(int a = 0) : _a(a) { cout << "Constructor A!" << _a << endl; } 7 ~A() { cout << "Destrucotr A!" << _a << endl; } 8 9private: 10 int _a; 11}; 12 13class B : public A { 14public: 15 B(int a = 0, int b = 0) : A(a), _b(b) { 16 cout << "Constructor B!" << _b << endl; 17 } 18 ~B() { cout << "Destrucotr B!" << _b << endl; } 19 20private: 21 int _b; 22}; 23 24class D { 25public: 26 D(int d = 0) : _d(d) { cout << "Constructor D!" << _d << endl; } 27 ~D() { cout << "Destrucotr D!" << _d << endl; } 28 29private: 30 int _d; 31}; 32class C : public B, public D { 33public: 34 C(int a = 0, int b = 0, int c = 0, int d = 0) : B(a, b), D(d), _c(c) { 35 cout << "Constructor C!" << _c << endl; 36 } 37 ~C() { cout << "Destrucotr C!" << _c << endl; } 38 39private: 40 int _c; 41}; 42int _tmain(int argc, _TCHAR *argv[]) { 43 B a(6), b(7, 8); 44 C c(1, 2, 3), d(12, 13, 14, 15); 45 return 0; 46} 47 48// OUtput: 49// Constructor A!6// Constructor B!0// Constructor A!7// Constructor 50// B!8// Constructor A!1// Constructor B!2// Constructor D!0// Constructor C!3// 51// Constructor A!12// Constructor B!13// Constructor D!15// Constructor C!14// 52// Destrucotr C!14// Destrucotr D!15// Destrucotr B!13// Destrucotr A!12// 53// Destrucotr C!3// Destrucotr D!0// Destrucotr B!2// Destrucotr A!1// 54// Destrucotr B!8// Destrucotr A!7// Destrucotr B!0// Destrucotr A!6