更新时间:2018-11-22 15:41作者:三水老师
A is speaking!
Destruct D
Destruct C
Destruct B
Destruct A
2.考察非虚析构函数这道题
class Parent
{
public:
Parent(){cout<<"Parent construct"<
~Parent(){ cout<<"Parent destruct "<
};
class Child : public Parent
{
public:
Child() { cout<<"Child construct "<
~Child() {cout<<"child destruct"<
};
int main()
{
Parent *p;
Child *c = new Child();
p = c;
delete p; // 因为析构函数是非 virtual 的,故析构的时候按照指针的类型进行析构
}
输出:
Parent construct
Child Construct
Parent destruct
3.考察初始化列表的写法
class A
{
public:
A(int x, int y, int z):a=x,b=y,c=z {} (1)
A(int x, int y, int z):a(x),b(y),c(z){} (2)
private:
int a;
int b;
int c;