简体   繁体   English

C ++虚拟析构函数

[英]C++ Virtual Destructors

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)? 如果我有一个基类和一个派生类,并且我在父虚拟中删除了析构函数,但是实例化了一个类型为subclass的对象,当它被销毁时它会调用父析构函数(从虚拟开始)? If I also declare a destructor in the derived class, will it call both destructors (base and derived). 如果我还在派生类中声明了析构函数,它是否会调用析构函数(base和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. 编辑:我的问题不仅仅是关于虚拟析构函数,而是为什么它需要被声明为虚拟,因为它们都应该默认调用。

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. 非常有限的情况下,如果你真的需要在vtable查找上保存几个周期,那么可以不虚拟化。

The need for virtual destructors is because of polymorphism. 对虚拟析构函数的需求是因为多态性。 If you have something like the following: 如果您有类似以下内容:

 class A { ... };

 class B : public A { ... };

 void destroy_class(A* input)
 {
     delete input;
 }

 int main()
 {
     B* class_ptr = new B();
     destroy_class(class_ptr); //you want the right destructor called

     return 0;
 }

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. 虽然有点人为的例子,当你删除destroy_class()函数的传入指针时,你想要调用正确的析构函数。 If the destructor for class A was not declared virtual , then only the destructor for class A would get called, not the destructor for class B or any other derived type of class A . 如果class A的析构函数未声明为virtual ,则只调用class A的析构函数,而不调用class B的析构函数或class 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, 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. 是的,首先调用子类析构函数,然后调用它的超类...然后它是超类,依此类推,直到我们找到Object的析构函数。

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. 更多信息: http//www.devx.com/tips/Tip/13059 ...值得一读...只有一个屏幕已满,但它是一个充满信息的屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM