C++虚析构和纯虚析构

 

 

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    //纯虚函数
    virtual void speak() = 0;

};

class Cat : public Animal {
public:
    void speak() {
        cout << "cat speak" << endl;
    }

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat();
    a->speak();
    delete a;
}

//下面是有问题代码

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    Animal() {
        cout << "Animal 构造函数" << endl;
    }

    ~Animal() {
        cout << "Animal 析构函数" << endl;
    }

    //纯虚函数
    virtual void speak() = 0;

};

class Cat : public Animal {
public:
    Cat(string n) {
        cout << "Cat 构造函数" << endl;
        name = new string(n);
    }

    ~Cat() {
        cout << "Cat 析构函数" << endl;
        if (name != NULL) {
            delete name;
            name = NULL;
        }
    }

    void speak() {
        cout << "cat speak" << endl;
    }

    string *name;

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat("Tom");
    a->speak();
    delete a;
}
Animal 构造函数
Cat 构造函数
cat speak

1、利用虚析构函数解决此问题

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    Animal() {
        cout << "Animal 构造函数" << endl;
    }

//利用虚析构可以解决 父类指针释放子类对象时不干净的问题
    virtual   ~Animal() {
        cout << "Animal 虚析构函数" << endl;
    }


    //纯虚函数
    virtual void speak() = 0;

};


class Cat : public Animal {
public:
    Cat(string n) {
        cout << "Cat 构造函数" << endl;
        name = new string(n);
    }

    ~Cat() {
        cout << "Cat 析构函数" << endl;
        if (name != NULL) {
            delete name;
            name = NULL;
        }
    }

    void speak() {
        cout << "cat speak" << endl;
    }

    string *name;

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat("Tom");
    a->speak();
    delete a;
}
Animal 构造函数
Cat 构造函数
cat speak
Cat 析构函数
Animal 虚析构函数

2、利用纯虚析构函数 解决此问题

#include <iostream>
#include <string>

using namespace std;

//虚析构和纯虚析构
class Animal {
public:
    Animal() {
        cout << "Animal 构造函数" << endl;
    }

//利用虚析构可以解决 父类指针释放子类对象时不干净的问题
//    virtual   ~Animal() {
//        cout << "Animal 虚析构函数" << endl;
//    }

    //或者纯虚析构  二者选其一
    //有了纯虚析构 之后,这个类也属于抽象类 无法实例化
    virtual  ~Animal() = 0;

    //纯虚函数
    virtual void speak() = 0;

};

Animal::~Animal() {
    cout << "Animal 纯虚析构函数" << endl;
}

class Cat : public Animal {
public:
    Cat(string n) {
        cout << "Cat 构造函数" << endl;
        name = new string(n);
    }

    ~Cat() {
        cout << "Cat 析构函数" << endl;
        if (name != NULL) {
            delete name;
            name = NULL;
        }
    }

    void speak() {
        cout << "cat speak" << endl;
    }

    string *name;

};

void test();

int main() {
    test();
    return 0;
}

void test() {
    Animal *a = new Cat("Tom");
    a->speak();
    delete a;
}
Animal 构造函数
Cat 构造函数
cat speak
Cat 析构函数
Animal 纯虚析构函数

 

引用\[1\]中提到,当子类中有属性开辟到堆区时,父类指针在释放时无法调用到子类的代码,导致子类中的堆区资源无法释放。为了解决这个问题,可以将父类中的函数改为或者的共性是可以解决父类指针释放子类对象时需要有具体的函数实现。 函数的定义如下: ```cpp class Animal { public: virtual ~Animal() { // 函数的具体实现 } }; ``` 函数的定义如下: ```cpp class Animal { public: virtual ~Animal() = 0; }; Animal::~Animal() { // 函数的具体实现 } ``` 引用\[3\]中给出了具体的代码实例,其中Animal类中的函数被定义为函数,而Cat类继承自Animal类并实现了具体的函数。这样,在使用父类指针释放子类对象时,会调用子类的函数,从而正确释放子类中的资源。 总结起来,函数函数都可以解决父类指针释放子类对象时资源无法释放的问题。函数需要有具体的实现,而函数需要在父类中声明并在父类外部实现。 #### 引用[.reference_title] - *1* *2* [12.7.6 ](https://blog.youkuaiyun.com/qq_32513033/article/details/107300809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C++的区别](https://blog.youkuaiyun.com/qq_51654808/article/details/124513776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值