cpp——类——析构函数

本文详细介绍了C++中的析构函数,包括其作用、特点以及在继承体系中的调用顺序。析构函数在对象销毁时自动调用,与构造函数的顺序相反。在类继承中,子类的析构函数先于父类执行,确保子类数据成员的正确销毁。同时,文章还提到了合成析构函数的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

析构函数特点:
  • 函数名为“~类名”,用来标记和识别析构函数
  • 没有返回值,析构函数是对自身的销毁行为,返回值无意义,因为没有返回值的接收者
  • 形参列表为空,所以析构函数没有重载,唯一的
构造函数VS析构函数:
  • 构造函数是对自身的初始化,生成(定义)对象时自动调用
  • 析构函数是对自身的销毁,销毁对象时自动调用

辅助类

class CAnimal
{
public:
    CAnimal() : mGroup(0)
    {
        cout << "CAnimal()" << endl;
    }
    
    CAnimal(int group) : mGroup(group)
    {
        cout << "CAnimal(" << group << ")" << endl;
    }
    
    CAnimal(const CAnimal &other) : mGroup(other.mGroup)
    {
        cout << "CAnimal(const CAnimal &other)" << endl;
    }
    
    ~CAnimal()
    {
        cout << "~CAnimal()" << endl;
    }
    
private:
    int mGroup;
};

class CDog : public CAnimal
{
public:
    CDog() : mLoyal(10)
    {
        cout << "CDog()" << endl;
    }
    
    CDog(int loyal) : CAnimal(1), mLoyal(loyal)
    {
        cout << "CDog(" << loyal << ")" << endl;
    }
    
    CDog(const CDog &other) : CAnimal(other), mLoyal(other.mLoyal)
    {
        cout << "CDog(const CDog &other)" << endl;
    }
    
    ~CDog()
    {
        cout << "~CDog()" << endl;
    }
    
private:
    int mLoyal;
};

class CCat : public CAnimal
{
public:
    CCat() : mCute(20)
    {
        cout << "CCat()" << endl;
    }
    
    CCat(int cute) : CAnimal(2), mCute(cute)
    {
        cout << "CCat(" << cute << ")" << endl;
    }
    
    CCat(const CCat &other) : CAnimal(other), mCute(other.mCute)
    {
        cout << "CCat(const CCat &other)" << endl;
    }
    
    ~CCat()
    {
        cout << "~CCat()" << endl;
    }
    
private:
    int mCute;
};

class CPig : public CAnimal
{
public:
    CPig() : mWeight(30)
    {
        cout << "CPig()" << endl;
    }
    
    CPig(int weight) : CAnimal(3), mWeight(weight)
    {
        cout << "CPig(" << weight << ")" << endl;
    }
    
    CPig(const CPig &other) : CAnimal(other), mWeight(other.mWeight)
    {
        cout << "CPig(const CPig &other)" << endl;
    }
    
    ~CPig()
    {
        cout << "~CPig()" << endl;
    }
    
private:
    int mWeight;
};

class CDonkey : public CAnimal
{
public:
    CDonkey() : mStrength(40)
    {
        cout << "CDonkey()" << endl;
    }
    
    CDonkey(int strength) : CAnimal(4), mStrength(strength)
    {
        cout << "CDonkey(" << strength << ")" << endl;
    }
    
    CDonkey(const CDonkey &other) : CAnimal(other), mStrength(other.mStrength)
    {
        cout << "CDonkey(const CDonkey &other)" << endl;
    }
    
    ~CDonkey()
    {
        cout << "~CDonkey()" << endl;
    }
    
private:
    int mStrength;
};

class CHorse : public CAnimal
{
public:
    CHorse() : mSpeed(50)
    {
        cout << "CHorse()" << endl;
    }
    
    CHorse(int speed) : CAnimal(5), mSpeed(speed)
    {
        cout << "CHorse(" << speed << ")" << endl;
    }
    
    CHorse(const CHorse &other) : CAnimal(other), mSpeed(other.mSpeed)
    {
        cout << "CHorse(const CHorse &other)" << endl;
    }
    
    ~CHorse()
    {
        cout << "~CHorse()" << endl;
    }
    
private:
    int mSpeed;
};

class CFarmLand
{
public:
    CFarmLand() : mArea(5000)
    {
        cout << "CFarmLand()" << endl;
    }
    
    CFarmLand(int area) : mArea(area)
    {
        cout << "CFarmLand(" << area << ")" << endl;
    }
    
    CFarmLand(const CFarmLand &other) : mArea(other.mArea)
    {
        cout << "CFarmLand(const CFarmLand &other)" << endl;
    }
    
    ~CFarmLand()
    {
        cout << "~CFarmLand()" << endl;
    }
    
private:
    int mArea;
};

class CFarm : public CFarmLand
{
public:
    CFarm() : mCapacity(1000)
    {
        cout << "CFarm()" << endl;
    }
    
    CFarm(int capacity) : mCapacity(capacity), mDog(20), mCat(30), mPig(40), CFarmLand(8000)
    {
        cout << "CFarm(" << capacity << ")" << endl;
    }
    
    CFarm(const CFarm &other) : mCapacity(other.mCapacity), mDog(other.mDog), mCat(other.mCat), mPig(other.mPig), CFarmLand(other)
    {
        cout << "CFarm(const CFarm &other)" << endl;
    }
    
    ~CFarm()
    {
        cout << "~CFarm()" << endl;
    }
    
private:
    int mCapacity;
    
private:
    CPig mPig;
    CDog mDog;
    CCat mCat;
};

析构顺序

void destruct()
{
    cout << "-----construct-----" << endl;
    CFarm farm;
    cout << "-----destruct-----" << endl;
}
output:
-----construct-----
CFarmLand()
CAnimal()
CPig()
CAnimal()
CDog()
CAnimal()
CCat()
CFarm()
-----destruct-----
~CFarm()
~CCat()
~CAnimal()
~CDog()
~CAnimal()
~CPig()
~CAnimal()
~CFarmLand()
结论:
  • 类继承体系中构造函数顺序:父类构造函数(初始化列表->函数体)->子类构造函数(初始化列表->函数体),析构函数顺序跟构造函数顺序恰好相反:子类析构函数(函数体->数据成员)->父类析构函数(函数体->数据成员)
  • 数据成员初始化顺序与数据成员在类中定义顺序一致,与初始化列表中初始化式顺序无关,数据成员析构顺序与数据成员初始化顺序恰好相反,即与数据成员在类中定义顺序相反
  • 子类可引用父类数据成员,反之父类不可引用子类数据成员,从这个逻辑也可推出子类析构先于父类析构,否则,如果父类析构先于子类析构,则子类析构函数访问了非法父类数据成员(已析构)

合成析构函数

如果没有显式定义析构函数,编译器会合成析构函数,一旦显式定义了析构函数(即使没有实现),编译器不再合成析构函数,编译器认为开发者知道怎么析构对象,不再需要编译器的帮助
合成析构函数函数体为空,其它与显式定义的析构函数无区别,依次调用类类型成员析构函数和父类析构函数
class CAdvanceFarm : public CFarm
{
private:
    CHorse mHorse;
    CDonkey mDonkey;
};
CAdvanceFarm没有显式定义析构函数,编译器会合成析构函数,合成析构函数为:
~CAdvanceFarm() {}
void destruct()
{
    cout << "-----construct-----" << endl;
    CAdvanceFarm farm;
    cout << "-----destruct-----" << endl;
}
output:
-----construct-----
CFarmLand()
CAnimal()
CPig()
CAnimal()
CDog()
CAnimal()
CCat()
CFarm()
CAnimal()
CHorse()
CAnimal()
CDonkey()
-----destruct-----
~CDonkey()
~CAnimal()
~CHorse()
~CAnimal()
~CFarm()
~CCat()
~CAnimal()
~CDog()
~CAnimal()
~CPig()
~CAnimal()
~CFarmLand()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值