直接用代码说明问题:
#include <iostream>
using namespace std;
class A {
public:
A(int a = 0) : _a(a) { cout << "Constructor A!" << _a << endl; }
~A() { cout << "Destrucotr A!" << _a << endl; }
private:
int _a;
};
class B : public A {
public:
B(int a = 0, int b = 0) : A(a), _b(b) {
cout << "Constructor B!" << _b << endl;
}
~B() { cout << "Destrucotr B!" << _b << endl; }
private:
int _b;
};
class D {
public:
D(int d = 0) : _d(d) { cout << "Constructor D!" << _d << endl; }
~D() { cout << "Destrucotr D!" << _d << endl; }
private:
int _d;
};
class C : public B, public D {
public:
C(int a = 0, int b = 0, int c = 0, int d = 0) : B(a, b), D(d), _c(c) {
cout << "Constructor C!" << _c << endl;
}
~C() { cout << "Destrucotr C!" << _c << endl; }
private:
int _c;
};
int _tmain(int argc, _TCHAR *argv[]) {
B a(6), b(7, 8);
C c(1, 2, 3), d(12, 13, 14, 15);
return 0;
}
// OUtput:
// Constructor A!6// Constructor B!0// Constructor A!7// Constructor
// B!8// Constructor A!1// Constructor B!2// Constructor D!0// Constructor C!3//
// Constructor A!12// Constructor B!13// Constructor D!15// Constructor C!14//
// Destrucotr C!14// Destrucotr D!15// Destrucotr B!13// Destrucotr A!12//
// Destrucotr C!3// Destrucotr D!0// Destrucotr B!2// Destrucotr A!1//
// Destrucotr B!8// Destrucotr A!7// Destrucotr B!0// Destrucotr A!6