CPlusPlusThings/practical_exercises/10_day_practice/day5/构造函数与析构函数/构造顺序.cpp
2023-01-02 20:39:00 +08:00

43 lines
568 B
C++

#include<iostream>
using namespace std;
/*
先构造成员
再构造自身(调用构造函数)
*/
class A {
public:
A() { cout<<"Constructing A"<<endl;}
~A(){ cout<<"Destructing A"<<endl;}
};
class B {
public:
B() { cout<<"Constructing B"<<endl;}
~B(){ cout<<"Destructing B"<<endl;}
};
class C
{
public:
C() { cout<<"Constructing C"<<endl;}
~C(){ cout<<"Destructing C"<<endl;}
B b;
A a;
};
int main()
{
C c;
system("pause");
}
/*
执行结果:
Constructing B
Constructing A
Constructing C
Destructing C
Destructing A
Destructing B
*/