1.析构函数:
~类名()
注意:析构函数没有返回值,没有参数(不能重载)
析构函数在销毁时自动调用
析构函数调用机制----c++编译器自动调用
public:
~Test(){
cout<<"~Test()"<<endl;
}
2.析构函数的功能
如果我们在类里面申请了一个指针并且为他开辟了一片堆上的内存空间,而堆上的内存空间又需要手动申请手动释放。如果我们没有释放就会导致内存泄漏。所以我们可以将释放指针空间的操作放在析构函数中。
public:
char *p=new char('a');
~Test(){
delete p;//c++释放空间的运算符
cout<<"~Test()"<<endl;
}
3.析构函数和构造函数的顺序
是先构造还是先析构呢?应该是先有构造后有析构,可以这么理解,先构造出,才能销毁掉嘛,下面我们来验证下吧
public:
Test(){
cout<<"Test()"<<endl;
}
~Test(){
delete p;
cout<<"~Test()"<<endl;
}
char *p=new char('a');
Test()
~Test()
输 出 量
从输出量可以明显看出,函数的执行顺序是先执行构造后执行析构。
4.多个构造和析构
当类中的成员变量是另一个类的实例化对象时,这个对象就叫做成员对象。
当成员对象所属的类没有无参构造函数时,需要使用初始化成员列表化
class Test{
public:
Test(){//无参构造函数
cout<<"Test()"<<endl;
}
Test(int x,int y):_x(x),_y(y)//有参构造函数
{
cout<<"Test(int x,int y)"<<endl;
}
~Test(){//析构函数
cout<<"~Test()"<<endl;
}
private:
int _x;
int _y;
};
class Test1{
public:
Test1(){
cout<<"Test1()"<<endl;
}
~Test1(){
cout<<"~Test1()"<<endl;
}
private:
Test abc1;//成员对象
Test abc2;
};
因为这里有默认构造函数,所以在执行Test1中Test实例化abc1时并没有报错
class Test{
public:
Test(int x,int y):_x(x),_y(y)//有参构造函数
{
cout<<"Test(int x,int y)"<<endl;
}
~Test(){//析构函数
cout<<"~Test()"<<endl;
}
private:
int _x;
int _y;
};
class Test1{
public:
Test1():abc1(10,20),abc2(20,30){//需要进行初始化对象列表
cout<<"Test1()"<<endl;
}
~Test1(){
cout<<"~Test1()"<<endl;
}
private:
Test abc1;//成员对象
Test abc2;//编译器不认识
};
Test1():abc1(10,20),abc2(20,30)这行代码的出现本质上是因为Test类中没有默认构造函数,而对于Test abc1;这条代码而编译器不认识,为了解决这个问题C++提供了初始化列表操作Test1():abc1(10,20),abc2(20,30)这里abc1里面的参数是类Test中的构造函数Test(int x,int y):_x(x),_y(y)所确定的
2.构造函数和析构函数的执行顺序
在有成员对象的类里面往往先执行成员对象的构造函数,在执行自己的构造函数
而析构函数则相反,先执行自己的析构函数,在执行成员对象的析构函数
class Test{
public:
Test(int x,int y):_x(x),_y(y)
{
cout<<"Test(int x,int y)"<<endl;
}
~Test(){
cout<<"~Test()"<<endl;
}
private:
int _x;
int _y;
};
class Test1{
public:
Test1():abc1(10,20){
cout<<"Test1()"<<endl;
}
~Test1(){
cout<<"~Test1()"<<endl;
}
private:
Test abc1;
};
Test(int x,int y)
Test1()
~Test1()
~Test()
输 出 量
由这个输出量就可以来啦