虚拟功能无法正常运行
可能的重复:
类具有虚拟函数和可访问的非虚拟析构函数
我从 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;
}
为什么我会收到这些警告:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器只是警告您,派生类的析构函数(由编译器添加)不是虚拟的。在这种情况下没有问题,您可以忽略该警告。但是,当派生类获得需要在析构函数中正确释放的其他数据元素时,您需要将析构函数设为虚拟,以便在 Enemy 指针上使用 delete 来调用它们(否则仅调用 Enemy 析构函数)。
编辑:当然,您不必在派生类的定义中将析构函数设为虚拟,而是在基类的定义中(或为了清楚起见而在两者中)。
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).
您的编译器敦促您为三个类添加虚拟析构函数。在这种特定情况下,这并不重要,但对于任何可以多态使用的类来说,拥有一个虚拟析构函数通常是一个好主意。
考虑以下示例:
在这里,您删除了指向 Enemy 实例的指针,该实例可能是派生类。派生类可能有自己的额外数据成员(在 Enemy 的数据成员之上)或特殊的初始化代码,但只有当 Enemy 及其子类具有虚拟析构函数时,
delete
才能访问此代码。否则,它将运行 Enemy 的析构函数,这可能会导致非常糟糕的结果。请注意,在您的代码中,Monster 和 Ninja 仅在堆栈上分配,并且它们不需要虚拟析构函数(因为编译器在编译时知道每个对象的完整类型)。但在实践中,动态分配多态对象(即具有公共虚函数的类的实例)是很常见的。因此,通常,您应该向此类类添加虚拟析构函数。
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:
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.