#include "stdafx.h"
class Test
{
public:
Test()
{
printf("test\n"); //第一个构造函数
}
Test(char* msg)
{
printf("test %s\n",msg); //第二个构造函数
}
};
class Child:public Test
{
public:
Child():Test("hello") //指明调用基类第二个构造函数
{
printf("child\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Child c; //显示 "test hello \n child \n"
return 0;
}
在派生类的构造函数后使用冒号,然后在这里调用基类的构造函数。
派生类中调用基类的构造函数
最新推荐文章于 2022-12-21 18:31:21 发布