c++ 初始化顺序

class Base
{
public:
	Base(int i) { cout << i; }
	~Base() { }
};
class Base1: virtual public Base
{
public:
	Base1(int i, int j=0) : Base(j) { cout << i; }
	~Base1(){}
};
class Base2: virtual public Base
{
public:
	Base2(int i, int j=0) : Base(j) { cout << i; }
	~Base2(){}
};
class Derived : public Base2, public Base1
{
public:
	Derived(int a, int b, int c, int d) : mem1(1), mem2(2), Base1(3),
		Base2(4),Base(1)
	{ cout << b; }
private:
	Base2 mem2;
	Base1 mem1;
};

int main(int argc, char **argv)
{
	 Derived objD (1, 2, 3, 4); 


	getchar();
	return 0;
}

  

  • 首先,任何虚拟基类的构造函数按照它们被继承的顺序构造;
  • 其次,任何非虚拟基类的构造函数按照它们被继承的顺序构造;
  • 最后,任何成员对象的构造函数按照它们声明的顺序调用;
原文地址:https://www.cnblogs.com/kaishan1990/p/5567789.html