#include "stdafx.h"
// 先调用父类的构造函数,再调用类内成员对象的构造函数,最后调用本类构造函数体
// 其中类内成员对象的构造函数调用顺序与定义顺序相同
class A
{
public:
A(int b)
{
printf("in class A%d\n",b);
}
};
class B
{
public:
B()
{
printf("in class B\n");
}
};
class C
{
public:
C()
{
printf("in class C\n");
}
};
class D:public A
{
public:
D():A(23)
{
a = 0;
printf("in class D\n");
}
private:
B b;
int a;
C c;
};
int _tmain(int argc, _TCHAR* argv[])
{
D d;
return 0;
// 先调用父类的构造函数,再调用类内成员对象的构造函数,最后调用本类构造函数体
// 其中类内成员对象的构造函数调用顺序与定义顺序相同
class A
{
public:
A(int b)
{
printf("in class A%d\n",b);
}
};
class B
{
public:
B()
{
printf("in class B\n");
}
};
class C
{
public:
C()
{
printf("in class C\n");
}
};
class D:public A
{
public:
D():A(23)
{
a = 0;
printf("in class D\n");
}
private:
B b;
int a;
C c;
};
int _tmain(int argc, _TCHAR* argv[])
{
D d;
return 0;
}
结果
in class A23
in class B
in class C
in class D