Virtual function table and C ++ abstract base class

Source: Internet
Author: User
I. Virtual function tableWhy is it feasible to use the pure abstract base class of C ++ to implement the COM interface? This is mainly because the memory structure defined in the pure abstract class can meet the interface requirements of COM.

When defining a pure abstract base class, it actually defines the structure of a memory block, and all its implementations are some memory blocks with the same basic structure. However, this memory is allocated only when the abstract base class is implemented in the derived class. The derived class that inherits the abstract base class also inherits the memory structure.

interface IX{   virtual void __stdcall fx1() = 0;   virtual void __stdcall fx2() = 0;   virtual void __stdcall fx3() = 0;   virtual void __stdcall fx4() = 0;};

As shown in figure 1, the memory structure defined by a pure abstract base class includes two parts: virtual function table vtable and pointer to vtable.
Pointer. The pointer to the abstract base class points to this vtable pointer, while the vtable Pointer Points to the virtual function table vtable. Vtable contains a set of pointers to the virtual function implementation. In example 1, the first item is the address of the fx1 function implemented in the derived class, and the second item is the address of fx2 ....

The memory structure of the COM interface is the same as the memory structure generated by the C ++ compiler for the abstract base class. Therefore, you can use abstract base classes to define com interfaces. The preceding com abstract base class IX is a COM interface because its memory structure complies with the requirements of the COM specification. However, there are other requirements for a COM interface. For example, all com interfaces must inherit an interface named iunkown. This means that the first three items of all com interfaces are the same, and the addresses of the three member functions in iunkown are saved.

II. Vtable Pointer and instance dataWhat is the purpose of the vtable pointer?

The vtable pointer adds an extra Level in the process from the abstract base class function pointer to the function, which brings great flexibility to the implementation of the interface.

When the C ++ compiler generates code, classes that implement abstract base classes may save instance-specific information with vtable. For example, CA implements the abstract base class IX. The Code is as follows:

class CA: public IX{public:    //implement interface IX    virtual void __stdcall fx1() {cout << “CA::fx1” << endl;}    virtual void __stdcall fx2() {cout << “CA::fx2” << endl;}    virtual void __stdcall fx3() {cout << “CA::fx3” << endl;}    virtual void __stdcall fx4() {cout << “CA::fx4” << endl;}     //constructor     CA(int i):m_ix1(i), m_ix2(i*i), m_ix3(i*i*i)     {}     //interface data     int m_ix1;     int m_ix2;     int m_ix3;};

For the compiler we mentioned above, the class data of vtable and CA will be shown in 2. It should be noted that the instance data should be accessible through the class pointer Pa, but because the customer generally does not know how the instance data is stored, therefore, customers cannot access them.


Although C ++ can directly operate and use instance data, the COM component will never access any instance data. In COM, access to a component can only be done through functions, but cannot be accessed directly through variables. In addition, the pure abstract base class only has virtual functions and does not have any instance data.

III:Multiple instances

The role of vtable is not only to provide a convenient location for the storage of instance data. In fact, different instances of the same class can share the same vtable. If we have created two different instances of CA, there will be two sets of different instance data, but different instances can share the same vtable and the same implementation (Figure 3 ). For example:

int main(){//first instance of CA.   CA* pA1 = new CA(2);//second instance of CA  CA* pA2 = new CA(3);}


However, although the COM component can use the vtable pointer to share the vtable, this is not necessary. Each instance of the COM component has a different vtable.

IV:Different classes, the same vtable 

The real power of an interface is that all classes that inherit this interface can be processed by the customer in the same way. For example, class CB also inherits IX:

Class CB: Public IX {public: // implement interface IX virtual void _ stdcall fx1 () {cout <"CB: fx1" <Endl ;} virtual void _ stdcall fx2 () {cout <"CB: fx2" <Endl;} virtual void _ stdcall FX3 () {cout <"CB :: FX3 "<Endl;} virtual void _ stdcall fx4 () {cout <" CB: fx4 "<Endl, customers can access Ca and CB through the same IX pointer. Void F (IX * pix) {pix-> fx1 (); pix-> fx2 ();} int main () {// create instance of CA * pA = new Ca (2); // create instance of CB * pb = new CB; // get Ix pointer to ca IX * pix = PA; F (pix); // get Ix pointer to CB pix = Pb; F (pix );}

Here, both CA and CB are used as IX interfaces, which is an example of polymorphism. The memory structure 4 in this example is shown. Both Ca and CB have instance data, vtable, and implementation. However, because their vtables have the same format, they can be accessed in the same way.


Figure 4 The format is generated by the compiler based on the definition of the abstract base class. When a class implements an abstract base class, it is forced to use this format. This is also true for components. When the component returns an iX interface pointer, it must ensure that the Pointer Points to the correct structure.

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.