Virtual function (virtual function) vtbl (virtual function table) and vptr (virtual function table pointer)

Source: Internet
Author: User

Virtual function (virtual function) vtbl (virtual function table) and vptr (virtual function table pointer)
Virtual function table (VirtualTable) vtbl vptr (virtual function table pointer) virtual pointer class virtual function table is a continuous memory, record the address of a JMP command in each memory unit
Note that the compiler will create a virtual function table for each class with virtual functions, which will be shared by all objects in the class. Each virtual member of the class occupies a row in the virtual function table. If there are N virtual functions in the class, the virtual function table will have N * 4 bytes. VirtualFunction is implemented through a VirtualTable. V-Table for short. In this table, it is mainly an address table of class virtual functions. This table solves the inheritance and overwrite issues and ensures that it truly reflects the actual functions. In this way, the table is allocated to the memory of the instance in instances of classes with virtual functions. Therefore, when the parent class pointer is used to operate a subclass, this virtual function table is very important. Like a map, it specifies the actually called function. The compiler should ensure that the pointer of the virtual function table exists at the top of the object instance (this is to ensure the highest performance of the obtained virtual function table-if there is multi-layer inheritance or multi-layer inheritance) ). This means that the virtual function table can be obtained through the address of the object instance, and then the function pointer can be traversed and the corresponding function can be called.

Adding virtual to the method definition indicates that this method is a virtual method that can be overwritten by sub-classes to modify the execution of the parent class.

Constructor cannot be virtual, because it is useless either to construct an object on the stack or to construct an object on the stack, whether or not you use the pointer or reference of the parent class to point to or reference this object in the future, during the "instant" of construction, it is always necessary to specify the specific type of the object to be constructed. Therefore, during the construction process, the object does not have the dynamic binding polymorphism during the runtime.

For example, if A is the parent class of B,
A * p = newB ();
For the virtual function f, you can directly call the Class B function through the pointer p of Class A, which is the polymorphism at runtime:
P-> f ();
Class B objects must be constructed by "A * p = newB ();". Obviously, they cannot be constructed by "A * p = newA (); it is absurd to construct A Class B object. This can only be used to construct A Class A object. Therefore, the constructor is meaningless.
However, the Destructor is different. p is A Class A pointer. If the Destructor is not virtual, you must delete the pointer safely in the following way:
Delete (B *) p;

However, if the constructor is virtual, You can dynamically bind the constructor to Class B at runtime, directly:
Deletep;
You can. This is the role of the virtual destructor. In fact, at runtime, You do not always know the actual type of the object referred to by p for forced conversion. Therefore, since C ++ supports polymorphism, you must also support virtual destructor.

The compiler always calls class member functions based on the type. However, the pointer of a derived class can be safely converted to a pointer of a base class. In this way, when a base class pointer is deleted, C ++ calls the base class destructor instead of the derived class, regardless of whether the pointer points to a base class object or a derived class object. If you rely on the code of the destructor of the derived class to release resources without reloading the destructor, there will be resource leakage.

Therefore, we recommend that you declare the Destructor as a virtual function. If you use MFC and use CObject or its derived class as the base class, MFC has already done this for you. The CObject destructor is a virtual function. Once declared as a virtual function, a function becomes a virtual function in all the derived classes regardless of whether or not you add the virtual modifier. However, for clarity, we recommend that you add virtual modifiers.

C ++ does not directly use virtual destructor as the default value because of the overhead of the virtual function table and its compatibility with the C language type. Objects with virtual functions always contain an implicit virtual function table pointer member at the beginning. If a pointer is added to a small class such as MFC CPoint and CSize, a large amount of memory is occupied, and the memory representation is inconsistent with that of the base class. If the two classes have the same memory, you can safely use the pointer or array of one class as the pointer or array of another class.

When the base class pointer is used to delete the object of the derived class, and the base class does not have a virtual destructor, the result is uncertain. This means that the code generated by the compiler will do whatever it like: reformat your hard disk, send an email to your boss, and fax your program source code to your opponent, everything can happen. (In actual operation, the destructor of the derived class will never be called.

To implement a virtual function, the object requires additional information so that the object can determine which virtual function to call at runtime. For most compilers, the specific form of this additional information is a pointer called vptr (virtual function table pointer. Vptr points to a function pointer array called vtbl (virtual function table. Each class with virtual functions has a vtbl. When calling a request for a virtual function of an object, the actually called function is determined by finding the corresponding function pointer in vtbl Based on the vptr pointing to vtbl.

The implementation details of virtual functions are not important, but the best scores in the base class are. at this point, the basic one is that declaring a virtual destructor for no reason is the same as never declaring it. In fact, many people have concluded that virtual destructor are declared only when the class contains at least one virtual function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.