c++--virtual function Table parsing

Source: Internet
Author: User

Turn from: 1948051

Objective

The function of virtual function in C + + is mainly to realize the mechanism of polymorphism. With regard to polymorphism, in short, it is to point to an instance of its child class with the parent type pointer, and then invoke the member function of the actual subclass through the parent type pointer. This technique allows the parent class to have a "multiple form" pointer, which is a generic technique. The so-called generic technology, plainly, is trying to use immutable code to implement a mutable algorithm. For example: template technology, RTTI technology, virtual function technology, either try to do at compile time resolution, or try to achieve runtime resolution.

On the use of virtual functions, I do not do much elaboration here. You can take a look at the relevant C + + books. In this article, I just want to from the virtual function of the implementation mechanism above for everyone a clear analysis.

Of course, the same article appeared on the Internet some, but I always feel that these articles are not very easy to read, large sections of the code, no pictures, no detailed instructions, no comparison, no extrapolate. Not conducive to learning and reading, so this is why I want to write this article. I hope you will give me more advice.

Let's go to the world of virtual functions.

virtual function table

You should know the virtual function (virtual Function

Here we focus on this virtual function table. The C + + compiler should be a pointer to the virtual function table that exists in the first place in the object instance (this is to ensure the highest performance of the virtual function table if there are multiple layers of inheritance or multiple inheritance). This means that we get this virtual function table from the address of the object instance, and then we can iterate over the function pointer and call the corresponding function.

Listen to me so much, I can feel it. You may be more disoriented than before. It doesn't matter, the following is the actual example, I believe that smart you see it.

Let's say we have a class like this:

1 classBase {2  Public:3     Virtual voidF () {cout <<"Base::f"<<Endl;}4     Virtual voidG () {cout <<"base::g"<<Endl;}5     Virtual voidH () {cout <<"base::h"<<Endl;}6};
View Code

According to the above, we can get the virtual function table through the example of base. Here's the actual code:

1#include <iostream>2 using namespacestd;3 4 classBase {5  Public:6     Virtual voidF () {cout <<"Base::f"<<Endl;}7     Virtual voidG () {cout <<"base::g"<<Endl;}8     Virtual voidH () {cout <<"base::h"<<Endl;}9 };Ten  Onetypedefvoid(*fun) (void); A  - voidMain () - { the Base B; -Fun Pfun =NULL; -cout <<"virtual function table address:"<< * (int*) (&AMP;B) <<Endl; -cout <<"virtual function table-First function address:"<< (int*)*(int*) (&AMP;B) <<Endl; +Pfun = (Fun) * ((int*)*(int*) (&b)); - Pfun (); +}
View Code

Note: Because the drawing is too laborious, it will not change. cout << "virtual function table-First function address:" << (int*) * (int*) (&b) << Endl; This sentence is changed to cout << "virtual function table-First function address:" << * (int*) * (int*) (&b) << Endl;

How do I call base::g () and base::h ()? The code is as follows

1#include <iostream>2 using namespacestd;3 4 classBase {5  Public:6     Virtual voidF () {cout <<"Base::f"<<Endl;}7     Virtual voidG () {cout <<"base::g"<<Endl;}8     Virtual voidH () {cout <<"base::h"<<Endl;}9 };Ten  Onetypedefvoid(*fun) (void); A  - voidMain () - { the Base B; -Fun PFUNF =NULL; -Fun Pfung =NULL; -Fun PFUNH =NULL; +cout <<"virtual function table address:"<< * (int*) (&AMP;B) <<Endl; -cout <<"virtual function table-First function address:"<< * (int*)*(int*) (&AMP;B) <<Endl; +PFUNF = (Fun) * ((int*)*(int*) (&AMP;B) +0); APfung = (Fun) * ((int*)*(int*) (&AMP;B) +1); atPFUNH = (Fun) * ((int*)*(int*) (&AMP;B) +2); - pfunf (); - Pfung (); - Pfunh (); -}
View Code

You should understand this time. What the? Still a little dizzy. Also, such code looks too messy. No problem, let me draw a diagram to explain. As shown below:

Note: In the above diagram, I added a node at the end of the virtual function table, which is the end node of the virtual function table, Just like the string Terminator "/0 winxp+vs2003 null ubuntu 7.10 + Linux 2.6.22 + GCC 4.1.3 1 0 Span lang= "ZH-CN" xml:lang= "ZH-CN" and represents the last virtual function table.

Below, I will explain the appearance of the virtual function table when there is no overwrite and overwrite. It is meaningless not to overwrite the virtual function of the parent class. I want to tell the story of the absence of coverage, the main purpose is to give a contrast. In comparison, we can know more clearly the specific implementation of its internal.

General Inheritance (no virtual function overrides)

Next, let's look at what the virtual function table looks like when inheriting. Suppose you have an inheritance relationship that looks like this:

Note that in this inheritance relationship, subclasses do not overload functions of any parent class. So, in an instance of a derived class, its virtual function table looks like this:

For example:Derive D; The virtual function table is as follows:

We can see the following points:

1) virtual functions are placed in the table in the order in which they are declared.

2) The virtual function of the parent class precedes the virtual function of the child class.

I believe that smart you can certainly refer to the previous program, to write a program to verify.

c++--virtual function Table parsing

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.