C++“接口”是否应该有一个虚拟析构函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3628529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should C++ 'interfaces' have a virtual destructor
提问by Mr. Boy
Possible Duplicate:
Destructors for C++ Interface-like classes
可能的重复:
C++ 类接口类的析构函数
Consider a simple example of a C++ abstract class, used to model an interface:
考虑一个 C++ 抽象类的简单示例,用于对接口建模:
class IAnimal
{
virtual void walk()=0;
virtual ~IAnimal(){}
};
Is it better to have the destructor, or not? I don't think the destructor can be pure virtual, at least my tests give linker errors, so should an empty destructor be included?
有析构函数更好,还是没有?我不认为析构函数可以是纯虚拟的,至少我的测试会给出链接器错误,所以应该包含一个空的析构函数吗?
EDIT:sorry, typo. It's a destructor not a constructor.
编辑:抱歉,打错了。它是一个析构函数而不是一个构造函数。
采纳答案by wheaties
You should always use a virtual destructor with interfaces. Case in point:
您应该始终使用带有接口的虚拟析构函数。案例:
IAnimal* animal = new Lion();
delete animal;
Now what destructor is it going to use? Definately not the Lion's destructor because the interface doesn't know about Lion's destructor.
现在它将使用什么析构函数?绝对不是 Lion 的析构函数,因为接口不知道 Lion 的析构函数。
So, have this if your interface has no memory management:
因此,如果您的界面没有内存管理,请使用这个:
virtual ~IAnimal(){}
回答by Chubsdad
Check out thisarticle by Herb Sutter
查看Herb Sutter 的这篇文章
Especially this part:
特别是这部分:
For the special case of the destructor only:
Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual.
仅对于析构函数的特殊情况:
准则#4:基类析构函数应该是public 和virtual,或者protected 和nonvirtual。
This assumes that base class is an 'interface' class as it mostly should be.
这假设基类是一个“接口”类,因为它主要应该是。
回答by Mike Seymour
This depends on whether you intend to manage the lifetime of objects polymorphically, using pointers to the interface class.
这取决于您是否打算使用指向接口类的指针以多态方式管理对象的生命周期。
If you do, then the destructor mustbe virtual, in order to correctly delete the objects. Deleting a base-class pointer that doesn't have a virtual destructor is invalid, and gives undefined behaviour.
如果这样做,则析构函数必须是虚拟的,以便正确删除对象。删除没有虚拟析构函数的基类指针是无效的,并且会产生未定义的行为。
If you don't, then you should enforce this by making the destructor non-virtual and protected, so only derived classes can be deleted.
如果不这样做,那么您应该通过使析构函数成为非虚拟和受保护来强制执行此操作,因此只能删除派生类。
回答by duffymo
I think it should be a pure virtual destructor for interfaces, and all other methods are pure virtual as well.
我认为它应该是接口的纯虚拟析构函数,所有其他方法也是纯虚拟的。
回答by gpeche
The only reason not to make the destructor virtual would be to save the space needed for the vptr
. As you need the vptr
anyway because you have another virtual function, I would make the destructor virtual.
不使析构函数成为虚拟的唯一原因是节省vptr
. 由于您vptr
有另一个虚函数,因此无论如何都需要它,因此我会将析构函数设为虚函数。
回答by flownt
an empty constructor should probably be included since a typical use of an interface involves putting a pointer to some concrete object in a container, which will otherwise call the wrong destructer and will not clean the memory correctly. so if anyone is going to delete derived objects through a pointer to Ianimal make a virtual destructor, else make your destructor nonvirtual and protected. making your destructor pure virtual is probably not such a good idea, since it forces implementers of derived classes to override your destructor, eventhough they might want to do nothing
可能应该包含一个空的构造函数,因为接口的典型用法涉及将指向某个具体对象的指针放在容器中,否则将调用错误的析构函数并且不会正确清理内存。因此,如果有人要通过指向 Ianimal 的指针删除派生对象,请创建一个虚拟析构函数,否则使您的析构函数成为非虚拟和受保护的。使您的析构函数纯虚拟可能不是一个好主意,因为它会强制派生类的实现者覆盖您的析构函数,即使他们可能什么都不做