class A{
public:
	A(){ cout << "this is A" << endl; }
	~A(){ cout << "delete A" << endl; }
};
class B{
public:
	B(){ cout << "this is B" << endl; }
	~B(){ cout << "delete B" << endl; }
};
class C :public B, A {
public:
	~C(){ cout << "delete C" << endl; }
	C(){ cout << "this is class C" << endl; }
};
int main()
{
	C temp;
	return 0;
}

运行结果为:

this is B
this is A
this is class C
delete C
delete A
delete B

 

1、C调用B,A的顺序是 由继承中声明的顺序决定的。

  public B ,A;  // 决定了先构造B,然后A

2、先调用父类的构造函数,然后再调用子类的构造函数

3、析构时先析构子类,再析构父类

 

相关文章:

  • 2021-07-29
  • 2021-11-08
  • 2021-10-07
  • 2022-12-23
  • 2021-09-27
  • 2021-12-20
猜你喜欢
  • 2022-12-23
  • 2021-10-13
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2022-03-02
相关资源
相似解决方案