C++虚拟析构函数
如果我有一个基类和一个派生类,并且我在父类 virtual 中删除了析构函数,但实例化了一个子类类型的对象,那么在销毁时它将调用父类析构函数(因为 virtual)?如果我还在派生类中声明一个析构函数,它是否会调用两个析构函数(基类和派生类)。提前致谢 :-)。
我的问题的第二部分是关于第一部分。为什么基类析构函数需要声明为virtual。构造函数不要循环向上的层次结构。他们没有同一个名字,那有什么必要呢?它对于析构函数不应该同样工作吗?或者默认情况下只调用一个?另外,通过后期绑定是否能够检测所有类和对象的构成?
编辑:我的问题不仅仅是关于虚拟析构函数,而是为什么它需要声明为虚拟的,因为默认情况下它们都应该被调用。
If I have a base class and a derived class, and I delcare the destructor in the parent virtual, but instantiate an object of type subclass, when destroyed it will invoke the parent destructor right(since virtual)? If I also declare a destructor in the derived class, will it call both destructors (base and derived). Thanks in advance :-).
The second part to my question is regarding the first. Why does the base class destructor need to be declared virtual. Don't constrcutors cycle up the hiearchy. They don't share the same name, so where's the need for it? Shouldn't it work the same for destrucotrs, or by default is only one called? Also does through late binding is it able to detect all the classes and object is made of?
EDIT: My question is not just about virtual destructors, but why does it need to be declared virtual, since they should all be called by default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,父析构函数将被自动调用。
析构函数应该被虚拟化,以便派生实例可以被认为具有对基类实例引用的代码正确地销毁。
在非常的情况下,如果您确实需要在vtable查找上节省一些周期,那么不进行虚拟化是可以的。
Yes, parent destructors will be called automatically.
The destructor should be virtualised so a derived instance can be destroyed properly by code that thinks it has a reference to a base class instance.
In very limited circumstances, it is OK not to virtualise, if you really need to save a few cycles on the vtable lookup.
需要虚拟析构函数是因为多态性。如果您有类似以下内容:
虽然有点人为的示例,但当您删除
destroy_class()
函数的传入指针时,您希望调用正确的析构函数。如果class A
的析构函数未声明为virtual
,则只会调用class A
的析构函数,而不是的析构函数B 类
或A 类
的任何其他派生类型。像这样的事情通常是非模板多态数据结构等的现实情况,其中单个删除函数可能必须删除实际指向派生类型对象的某些基类类型的指针。
The need for virtual destructors is because of polymorphism. If you have something like the following:
While a bit of a contrived example, when you delete the passed-in pointer for the
destroy_class()
function, you want the correct destructor to get called. If the destructor forclass A
was not declaredvirtual
, then only the destructor forclass A
would get called, not the destructor forclass B
or any other derived type ofclass A
.Stuff like this is very often a fact-of-life with non-template polymorphic data-structures, etc. where a single deletion function may have to delete pointers of some base-class type that actually points to an object of a derived type.
rubixibuc,
是的,首先调用子类析构函数,然后是超类...然后是超类,依此类推,直到我们到达对象的析构函数。
更多信息请参见: http://www.devx.com/tips/Tip/13059 。 ..值得一读...虽然只是满屏,但内容丰富。
rubixibuc,
Yeah the subclasses destructor is invoked first, then it's superclass... then it's superclass, and so on until we get to Object's destructor.
More here: http://www.devx.com/tips/Tip/13059 ... it's worth the read... only a screen-full, but it's an INFORMATIVE screen-full.