Virtual function not functioning properly [duplicate]

霸气de小男生 提交于 2019-12-12 01:49:54

问题


Possible Duplicate:
class has virtual functions and accessible non-virtual destructor

I got this code from following a tutorial by thenewboston:

#include <iostream>

using namespace std;

class Enemy {
public:
    virtual void attack(){};
};

class Ninja: public Enemy {
public:
    void attack(){
        cout << "ninja attack"<<endl;
    }
};

class Monster: public Enemy {
public:
    void attack(){
        cout << "monster attack"<<endl;
    }
};

int main() {
    Ninja n;
    Monster m;
    Enemy * enemy1 = &n;
    Enemy * enemy2 = &m;
    enemy1->attack();
    enemy2->attack();
    cin.get();
    return 0;
}

Why do I get these warnings:

class Enemy has virtual functions and accessible non-virtual destructor
class Ninja has virtual functions and accessible non-virtual destructor
class Monster has virtual functions and accessible non-virtual destructor

回答1:


Your compiler is urging you to add a virtual destructor for your three classes. In this specific case, it doesn't really matter, but it's generally a good idea to have a virtual destructor for any class that may be used polymorphically.

Consider the following example:

class Fight
{
public:
    Fight(Player* player, Enemy* enemy)
    {
        m_player = player;
        m_enemy  = enemy;
    }

    void playerAttack()
    {
        m_player.attack(m_enemy);
        if (m_enemy.isDead())
            delete m_enemy;
    }

private:
    Player* m_player;
    Enemy* m_enemy;
}

Here you delete a pointer to an Enemy-instance, which is probably a derived class. The derived class may have its own extra data members (on top of Enemy's data members) or special initialization code, but delete can only reach this code if Enemy and its child classes have virtual destructors. Otherwise, it would run Enemy's destructor, which may lead to very bad results.

Note that in your code, Monster and Ninja only ever get allocated on the stack, and they won't need virtual destructors (since the compiler knows the full-type of each object at compilation time). But in practice, it's very common to dynamically allocate polymorphic objects (i.e. instances of classes with public virtual functions). So as a rule, you should add virtual destructors to this kind of classes.




回答2:


The compiler just warns you, that the destructors of your derived classes (added by the compiler) are not virtual. In this case it is no problem and you can ignore the warning. But when your derived classes get additional data elements that need to be released properly in the destructor, you need to make the destructors virtual, so they get called using delete on an Enemy pointer (otherwise only the Enemy destructor gets called).

EDIT: Of course you do not have to make the destructors virtual in the derived classes' definitions, but in the base class's (or in both for clarity).



来源:https://stackoverflow.com/questions/5931203/virtual-function-not-functioning-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!