C++构造函数初始化顺序
作者:网络转载 发布时间:[ 2015/7/30 10:47:50 ] 推荐标签:测试开发技术 开发语言
C++构造函数按下列顺序被调用:
(1)任何虚拟基类的构造函数按照它们被继承的顺序构造;
(2)任何非虚拟基类的构造函数按照它们被继承的顺序构造;
(3)任何成员对象的构造函数按照它们声明的顺序调用;
(4)类自己的构造函数。
#include <iostream>
using namespace std;
class OBJ1
{
public:
OBJ1(){ cout <<"OBJ1
"; }
};
class OBJ2
{
public:
OBJ2(){ cout <<"OBJ2
"; }
};
class Base1
{
public:
Base1(){ cout <<"Base1
"; }
};
class Base2
{
public:
Base2(){ cout <<"Base2
"; }
};
class Base3
{
public:
Base3(){ cout <<"Base3
"; }
};
class Base4
{
public:
Base4(){ cout <<"Base4
"; }
};
class Derived :public Base1, virtual public Base2,
public Base3, virtual public Base4
{
public:
Derived() :Base4(), Base3(), Base2(),
Base1(), obj2(), obj1()
{
cout <<"Derived ok.
";
}
protected:
OBJ1 obj1;
OBJ2 obj2;
};
int main()
{
Derived aa;
cout <<"This is ok.
";
int i;
cin >> i;
return 0;
}
结果:
Base2
Base4
Base1
Base3
OBJ1
OBJ2
Derived ok.
This is ok.
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11