什么是Virtua解构器? [英] What is Virtua Deconstructor?

查看:69
本文介绍了什么是Virtua解构器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你在C ++中使用虚拟析构函数是什么意思?什么时候调用它们?

对象中的vtable和vptr条目是什么以及它们是如何初始化的?



还举一些例子..

What do you mean by virtual destructor in C++ and when they are invoked??
What are the vtable and vptr entries in the object and how they are initialized??

Please Give some examples also..

推荐答案

没有解构主义者这样的东西。它被称为析构函数。



如果方法是虚拟的,那么在对象上调用方法总是调用由派生程度最高的类实现的方法。如果方法不是虚拟的,然后执行对应于对象指针的编译时类型。



http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx [ ^ ]
There is no such thing as a deconstructor. It''s called a destructor.

"If a method is virtual, then calling the method on an object always invokes the method as implemented by the most heavily derived class. If the method is not virtual, then the implementation corresponding to the compile-time type of the object pointer. "

http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx[^]


构造始终从基础派生到派生。并且应该从派生到基地发生破坏。如果您没有使用虚函数机制,它将按预期工作,但请考虑以下示例,



Construction always happens from base to derived. And destruction ''should'' happen from derived to base. If you are not using virtual function mechanism, it will work as expected, but consider following example,

base *p = new derived();





这里的编译器假设,p是''base''类型。因此,除非你将析构函数设为虚拟,否则它不会调用派生的析构函数。这可能导致内存泄漏。



Here compiler assumes that, p is ''base'' type. So unless you make destructor virtual, it will not call derived''s destructor. Which may led to memory leak.


所有这些实体仅存在于具有虚函数的类中,或者来自具有的类。



对于非虚函数,编译器可以在编译时解析任何函数调用。它被转换为跳转到代码库的不同地址。但是对于虚函数,必须调用的函数取决于调用函数的对象的类类型,并且在编译时可能不知道该类型。



作为在运行时确定这些内容的帮助,编译器为每个具有虚函数的类生成一个vtable(显然在编译时)。该表包含指向此类方法的函数指针。在运行时,每当创建具有虚拟函数的类的对象时,它将使用指向与其类对应的函数表的指针初始化。当在该对象上调用潜在的虚方法时,运行时系统将跟随函数表指针,然后在引用的vtable中查找函数。



具体细节vtable和vptr中的条目如何看起来取决于编译器。对于不同版本的编译器,它可能会有所不同。它甚至可能根据班级的确切类型而有所不同!作为程序员,除非你自己开发编译器,否则你不应该关心它。在这种情况下,您可以尝试查看特定的编译器文档。
All of these entities only exist for classes that have virtual functions, or are derived from a class that has.

For non-virtual functions, the compiler can resolve any function call at compile time. It is translated into a jump to a different address of the code base. For virtual functions however, the function that has to be called depends on the class type of the object that the function is called on, and that type may not be known at compile time.

As a help to determine these things at run time, the compiler generates a vtable for each class with virtual functions (at compile time, obviously). That table contains function pointers to the methods of this class. At runtime, whenever an object of a class with virtual fucntions is created, it will be intialized with a pointer to that function table corresponding to its class. When a potentially virtual method is called on that object, the runtime system will follow the function table pointer and then look up the function within the referenced vtable.

The exact details of how the entries in a vtable and a vptr look like, depends on the compiler. It may vary for different versions of a compiler. It may even vary depending on the exact type of the class! As a programmer, you shouldn''t care about that, unless you''re developing a compiler yourself. In that case, you could try and check out the specific compiler documentation.


这篇关于什么是Virtua解构器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆