Anyone who knows about C ++ in the virtual function table should know the virtual function.

Source: Internet
Author: User

Anyone who knows about C ++ in the Virtual Function Table 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 overload problems and ensures that it can reflect the actual function. 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 function table. In the standard specification of C ++, the compiler must ensure that the pointer to the virtual function table exists in the front of the object instance (this is 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:

The role of virtual functions in C ++ is to implement the polymorphism mechanism. With regard to polymorphism, in short, the pointer of the parent type points to the instance of its subclass, and then calls the member function of the actual subclass through the pointer of the parent class. This technology enables the pointer of the parent class to have multiple forms. This is a generic technology. 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.

Class Base {

Public:

Virtual void f () {cout <"Base: f" <endl ;}

Virtual void g () {cout <"Base: g" <endl ;}

Virtual void h () {cout <"Base: h" <endl ;}

};

 

As mentioned above, we can use the Base instance to obtain the virtual function table. The following is the actual routine:

 

Typedef void (* Fun) (void );

Base B;

Fun pFun = NULL;

Cout <"virtual function table address:" <(int *) (& B) <endl;

Cout <"virtual function table-first function address:" <(int *) * (int *) (& B) <endl;
 

// Invoke the first virtual function

PFun = (Fun) * (int *) (& B ));

PFun ();

 

The actual running results are as follows: (Windows XP + VS2003, Linux 2.6.22 + GCC 4.1.3)

Author: "fliggy Chuan Shu 2011, a fully open-source chat software developed by C ++"

Related Article

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.