Analytic dynamic Joint (next article)

Source: Internet
Author: User

Three-virtual function table vtable

The dynamic binder process is roughly the same as we guess. When the compiler encounters the virtual keyword during execution, it will automatically install the mechanism required by the dynamic binder, starting with the class containing the virtual function (note that is not an instance of the class)-even if the ancestor class contains a virtual function and does not itself-create a virtual function table vtable. In these virtual function tables, the compiler places the address of a specific virtual function of a class in the order of the function declaration in turn. At the same time, place a pointer called Vpointer in each class with a virtual function, referred to as vptr, which points to the vtable of the class.

For a list of virtual functions, there are several points that must be clearly stated:

1. Each category can have only one virtual function table, and if the class does not have a virtual function, there is no virtual function table.

2. C + + compile time the compiler adds a pointer vptr to the virtual function table in a class that contains a dummy function.

3. Each object that is born from a category will get the vptr pointer in that category, which also points to the VTABLE of the class.

So the hierarchy of classes, objects, and vtable can be represented in the following figure. The objects in the X and Y classes all point to the x,y virtual function table, while the X,y class itself contains pointers to virtual functions.

For the convenience of the problem description, we extend the 2.cpp example to extend the program as follows.//4.cpp
15. #include <iostream.h >
16. class shape{
17.  public:
18.  virtual void draw(){cout<<"shape::draw ()"<<endl;}
19.  virtual void area() {cout<<"shape::area()"<<endl;}
20.  void fun(){draw();area();}
21. };
22. class circle:public shape {
23. public:
24.  void draw() {cout<<"circle::draw()"<<endl;}
25.  void adjust(){cout<<"circle::adjust()"<<endl;}
26. };
27. main(){
28.  shape oneshape;
29.   oneshape.fun();
30.
31.  circle circleshape;
32.   shape& baseshape=circleshape;
33.  baseshape.fun();
34. }

When compiling the above code, the compiler will create a vtable table for the shape and circle two objects, which in turn populate all the virtual function addresses declared in the derived class object and the base class object. If the derived class itself does not redefine the virtual function of the base class, then the virtual function address of the base class is populated. This allows the base class method to be invoked automatically when a function calls a method that does not exist for a derived class. The compiler then places a vptr in each class, typically at the beginning of the object, and then initializes vptr to the vtable address of the class in the object's constructor. The entire result is laid out as follows.

Figure I

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.