C++虚拟析构函数

发布于 2024-11-03 11:22:31 字数 334 浏览 5 评论 0原文

如果我有一个基类和一个派生类,并且我在父类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万水千山粽是情ミ 2024-11-10 11:22:31

是的,父析构函数将被自动调用。

析构函数应该被虚拟化,以便派生实例可以被认为具有对基类实例引用的代码正确地销毁。

非常的情况下,如果您确实需要在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.

瑾夏年华 2024-11-10 11:22:31

需要虚拟析构函数是因为多态性。如果您有类似以下内容:

 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;
 }

虽然有点人为的示例,但当您删除 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:

 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. 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.

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.

妄司 2024-11-10 11:22:31

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文