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 trying to use the sameCodeTo implement variableAlgorithm. 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 articleArticle.

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)

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.