How to Implement Virtual functions in the C ++ virtual function table

Source: Internet
Author: User

The role of virtual functions in the C ++ language is to implement the polymorphism mechanism. The virtual functions in C ++ are implemented through a C ++ virtual function table. This table solves the inheritance and overload problems and ensures that the table can reflect the actual functions.

The so-called generic technology, to put it bluntly, is to try to use the same code to implement a variable algorithm. For example, the template technology, RTTI technology, and virtual function technology either try to achieve resolution at compilation or at runtime. I will not elaborate on the usage of virtual functions too much here. You can read related C ++ books. In this article, I just want to give you a clear analysis of the implementation mechanism of virtual functions.

Of course, the same article has also appeared on the Internet, but I always feel that these articles are not very easy to read. There are no pictures, no detailed descriptions, and no comparison, no. It is not conducive to learning and reading, so this is why I want to write this article. I hope you will give me more comments. Let's get down to the truth and let us enter the world of virtual functions together.

Virtual function table

Anyone who knows about C ++ should know that Virtual functions are implemented through a Virtual Function Table. V-Table for short. In this table, the primary table is the address table for a class virtual function. This table solves the inheritance and overwrite issues and ensures that it can reflect 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 we use the parent class pointer to operate a subclass, this virtual function table is very important. Like a map, it specifies the actually called function.

Here we will focus on this virtual C ++ virtual function table. According to the standard specification in C ++, the compiler must ensure that the pointer of the virtual function table exists in the first position in the object instance to ensure that the offset of the virtual function is obtained correctly ). This means that we can get this virtual function table through the address of the object instance, then we can traverse the function pointer and call the corresponding function.

After hearing that, I can feel that you are more dizzy than before. It doesn't matter. The following is an actual example. I believe you will understand it at a glance. Suppose we have a class like this:

 
 
  1. class Base {   
  2.  
  3. private:   
  4.  
  5. virtual void f() { cout << "Base::f" << endl; }   
  6.  
  7. };   
  8.  
  9. class Derive : public Base{   
  10.  
  11. };   
  12.  
  13. typedef void(*Fun)(void);   
  14.  
  15. void main() {   
  16.  
  17. Derive d;   
  18.  
  19. Fun pFun = (Fun)*((int*)*(int*)(&d)+0);   
  20.  
  21. pFun();   
  22.  
  23. }  

C ++ is a Magic language. For programmers, we can never find out what we are doing with this language. To be familiar with this language, we must understand the things in C ++ and the dangerous things in C ++. Otherwise, this is a type of C ++ virtual function table with a stone hitting its own foot.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.