Virtual table mechanism

Tram Ho

Virtual table mechanism

1. Definition

  • Virtual tables are important mechanisms in object-oriented programming, used in dealing with virtual functions.
  • In order to implement the virtual function when building classes, C ++ uses a special binding mechanism (late / late binding: which is enforced at run time) called virtual table (also known as “vtable”, “virtual function table”.) Virtual table is a mechanism used to resolve function calls dynamically.


-> The virtual table mechanism allows making calls to function1 (), function2 (defined in class D1, D2) of objects d1 and d2.

2. How it works

– Before looking at the activities of virtual, we will learn about what the _vptr pointer is?

  • The _vptr pointer is a pointer that is created every time an object (the class that defines this object has a virtual function declaration) is created.
  • When an object is created, the _vptr pointer will also be created to point to the object’s virtual table. eg object b is created, then _vptr pointer is also declared to point to the virtual table of object b.

– How is the virtual table created?

  • In the above example, because in the Base class, there are 2 functions defined virtual so each table will have 2 items (1 for function1 () and 1 for function2 ()).
  • The virtual table of Base objects is very simple, a Base object can only access members of the Base class>, so the entry function1 () points to Base :: function1 () and the entry for function2 () Point to Base :: function2 ().
  • Virtual tables for D1 objects are more complex, a D1 object is accessible to members of the Base and D1 classes. Function1 () was overridden in class D1, so the entry for function1 () will point to D1 :: function1 (), and function2 () is not defined, so the entry for function2 () points to Base :: function2 ().
  • Similarly, objects of type D2, the function1 () entry point to Base :: function1 (), and the entry for function2 () points to D2 :: function2 ().

-> When calling function1 () and function2 () of objects of type D1 and D2, the _vptr pointer performs the task to call the exact function to call.

The virtual table diagram in the example above

3. Conclusion

  • Virtual table is an extremely important mechanism in object-oriented programming, it completely solves the processing of virtual functions, even when using only a pointer or reference to a base class.
  • Virtual function calls will be slow to call regular functions for a number of reasons:
    • The first is the use of the _vptr pointer to get to the virtual version and call the appropriate function.
    • Second, we have to index the virtual table to find the right function to call, then we can call the right function.

    -> Result in having to perform 3 operations to find the function that needs to call more than calling 1 operation for the regular function call.

  • Using virtual functions is very powerful but they will consume resources as each object will have an additional pointer.

You can refer to the following basic code and guess if the function is called! To better understand virtual functions and virtual tables.


Chia sẻ bài viết ngay

Nguồn bài viết : Viblo