析构函数的作用:
在对象撤销前做必要的“清理现场”的工作,当派生类的对象从内存中撤销时,一般先调用派生类的析构函数,再调用基类的析构函数,但是如果,用new运算符动态生成一个派生类的堆对象,并让基类指针指向该派生类对象,当程序用delete运算符通过基类指针撤销派生类对象时,系统只会执行基类的析构函数,而不执行派生类的析构函数
基类中有非虚构函数时的执行情况:
#include<iostream>
using namespace std;
class point
{
public:
point(){cout<<"point constructor"<<endl;}
~point(){ cout<<"executing point destructor"<<endl;}
};
class circle:public point
{
public:
circle(double r):radius(r){cout<<"circle constructor"<<endl;}
~circle(){ cout<<"executing circle destructor"<<endl;}
private:
double radius;
};
int main()
{
point *p=new circle(2.0);
delete p;
system("pause");
}
执行结果:
上面只执行了基类point的析构函数,而没有执行派生类circle的析构函数,如果希望能执行派生类的析构函数,则应该将基类的析构函数声明为虚析构函数
如:
#include<iostream>
using namespace std;
class point
{
public:
point(){cout<<"point constructor"<<endl;}
virtual~point(){ cout<<"executing point destructor"<<endl;}
};
class circle:public point
{
public:
circle(double r):radius(r){cout<<"circle constructor"<<endl;}
~circle(){ cout<<"executing circle destructor"<<endl;}
private:
double radius;
};
int main()
{
point *p=new circle(2.0);
delete p;
system("pause");
}
执行结果:
先调用了派生类的析构函数,再调用了基类的析构函数,当基类的析构函数为虚函数时,无论指针指向的是同一类族中哪个对象,系统都会采用动态关联,调用相应的析构函数,对该对象进行清理工作。
如果将基类的析构函数声明为虚函数时,由该基类所派生的所有派生类的析构函数也都自动成为虚函数,即使派生类的析构函数与基类的析构函数名字不同,最好将基类的析构函数声明为虚函数,这将使所有派生类的析构函数自动成为虚函数,
即使基类不需要析构函数,也显示地定义一个函数体为空的虚析构函数,以保证在撤销动态分配空间时能得到正确的处理。
栈中对象-----构造函数与析构函数的调用
#include<iostream>
using namespace std;
class point
{
public:
point(){cout<<"point constructor"<<endl;}
virtual ~point(){ cout<<"executing point destructor"<<endl;}
};
class circle:public point
{
public:
circle(double r):radius(r){cout<<"circle constructor"<<endl;}
~circle(){ cout<<"executing circle destructor"<<endl;}
private:
double radius;
};
int main()
{
{
cout<<"point 栈对象,构造函数与析构函数"<<endl;
point p ;
}
{
cout<<"circle 栈对象,构造函数与析构函数"<<endl;
circle c(2.0);
}
system("pause");
}
执行结果:
当构造子类对象时,先调用父类构造函数,再调用子类构造函数,析构的时候,先调用子类析构函数,再调用父类的析构函数
堆对象---构造函数和析构函数的调用情况
(new 调用构造函数,delete调用析构函数,如果只new不delete,即使出了对象作用域,对象也不会析构,因为new分配的内存在堆空间)
#include<iostream>
using namespace std;
class point
{
public:
point(){cout<<"point constructor"<<endl;}
virtual ~point(){ cout<<"executing point destructor"<<endl;}
};
class circle:public point
{
public:
circle(double r):radius(r){cout<<"circle constructor"<<endl;}
~circle(){ cout<<"executing circle destructor"<<endl;}
private:
double radius;
};
int main()
{
{
cout<<"point 堆对象"<<endl;
point *p =new point();
delete p;
}
{
cout<<"circle 堆对象"<<endl;
circle *c =new circle(2.0);
delete c;
}
{
cout<<"circle 堆对象"<<endl;
point *p =new circle(2.0);
delete p;
}
system("pause");
}
执行结果: