小编낑낑깡*_*낑낑깡的帖子

这是抽象的,但具有非虚拟析构函数删除导致错误。这是为什么?

#include <iostream> 
using namespace std;

class Animal{
public:
 virtual void cry() = 0;
 void eat();
};


class Cat:public Animal
{
public:
        virtual void cry();
        void grooming();
};


class Dog:public Animal
{
public:
        virtual void cry();
        void lash();
};


main()
{
    Animal* animal = new Cat();
    animal->eat();
    animal->cry();

    Cat* cat = (Cat*)animal;
    cat->grooming();

    Dog* dog = new Dog();
    dog->cry();
    dog->eat();
    dog->lash();

    delete animal;     //Delete called on'animal' that is abstract but has non-virtual destruct
    delete cat;             //Thread 1: signal SIGABRT
    delete dog; …
Run Code Online (Sandbox Code Playgroud)

c++

6
推荐指数
1
解决办法
8082
查看次数

标签 统计

c++ ×1