使用基类指针创建对象时,缺少派生类析构函数 [英] Missing Derived class Destructor when object is created with the base class pointer

查看:76
本文介绍了使用基类指针创建对象时,缺少派生类析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码示例中,未调用派生类析构函数。知道为什么吗? 我有一个具有虚函数的基类。现在,我使用基类指针来创建派生类的新对象。我的理解是,当销毁派生类对象时,首先调用派生类的析构函数,然后调用基类。然而,我只看到基类的析构函数被调用。有人知道我做错了什么吗,或者我对C++的理解在哪里不正确?

#include <iostream>
#include <bitset>
using namespace std; 

class DomesticAnimals 
{
    public:
        DomesticAnimals() {
            cout <<"Domestic Animal Base" <<endl;
        }; 
        ~DomesticAnimals() {
            cout <<"Domestic Animal kill " <<endl;
        }; 
        virtual void Speak() = 0;
        virtual void EatFood()= 0; 
        virtual int NoOfLegs() {
            return 4; 
        } ;
};


class Cat : public DomesticAnimals
{
    public:
        Cat(); 
        ~Cat(); 
        void Speak() override; 
        void EatFood() override; 
};

Cat::Cat()
{
    cout << "Kat was born" << endl;
}

Cat:: ~Cat()
{
    cout << "Kill de Cat" << endl; 
}
void Cat:: Speak()
{
    cout << "Meow Meow " << endl; 
}

void Cat::EatFood()
{
    cout <<"Kat eet de muis vandaag !! " <<endl; 
}


class Dog : public DomesticAnimals
{
    public:
        Dog();
        ~Dog();
        void Speak() override; 
        void EatFood() override; 
};

Dog::Dog()
{
    cout << "A puppy was born" << endl; 
}

Dog::~Dog()
{
    cout << "Kill de hond" << endl; 
}

void Dog :: Speak()
{
    cout <<"bow bow woof woof" <<endl;
}

void Dog :: EatFood()
{
    cout << "de hond eet een kip voor middageten" <<endl;
}

void DogActions()
{
    DomesticAnimals* dog = new Dog;
    cout<< endl;
    dog->Speak(); 
    dog->EatFood();
    cout<<"No of legs for dog = "<< dog->NoOfLegs() <<endl; 
    cout<<endl;
    delete dog; 
    dog = NULL;
}

void CatActions()
{
    DomesticAnimals* cat = new Cat; 
    cat->Speak(); 
    cat->EatFood(); 
    cout<<"No of legs for cat = "<< cat->NoOfLegs() << endl;
    delete cat; 
    cat = NULL; 
}

int main(void)
{
    DogActions();
    CatActions();
    return 0;
}

程序的输出如下

Domestic Animal Base
A puppy was born

bow bow woof woof
de hond eet een kip voor middageten
No of legs for dog = 4

Domestic Animal kill 
Domestic Animal Base
Kat was born
Meow Meow 
Kat eet de muis vandaag !! 
No of legs for cat = 4
Domestic Animal kill 

推荐答案

基类的析构函数需要是虚的:

virtual ~DomesticAnimals() {
    cout << "Domestic Animal kill" << endl;
};

为了避免混淆(参见注释):只有在通过指向其基类的指针删除对象的情况下,才需要将析构函数设为虚的。当需要多态时,通常是这种情况。

这篇关于使用基类指针创建对象时,缺少派生类析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆