Preface
The function of virtual function in C + + is mainly to realize the mechanism of polymorphism. In terms of polymorphism, in short, a pointer to the parent type points to an instance of its child class, and then invokes the member function of the actual subclass through a pointer to the parent class. 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
Anyone who knows about C + + should know that virtual functions (virtual function) are passed through a list of virtual functions ( Virtual Table) to implement. Referred to as v-table.
In this table, the master is the Address table of the virtual function of a class, which solves the problem of inheritance, overwriting, and guarantees the actual function of the real response. Thus, in an instance of a class with virtual functions, the table is assigned to the memory of this instance, so when we manipulate a subclass with a pointer to the parent class, the virtual function table is important, and it is like a map that indicates the function that should actually be called.
Note: Non-virtual functions are not in the virtual function table.
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:
classBase { Public: Virtual voidF () {cout <<"Base::f"<<Endl;} Virtual voidG () {cout <<"base::g"<<Endl;} Virtual voidH () {cout <<"base::h"<<Endl;} };
According to the above, we can get the virtual function table through the example of Base. here is the actual routine:
typedefvoid(*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 functionPfun = (Fun) * ((int*)*(int*) (&b)); Pfun ();
The actual run-through results are as follows:(Windows xp+vs2003, Linux 2.6.22 + GCC 4.1.3)
virtual function table Address: 0012FED4
virtual function table-First function address: 0044f148
Base::f
Through this example, we can see that we can force &b int * base::f () INT*&NBSP; forced to function pointer). With this example, we can know if you want to call base::g () base::h ()
(Fun) * ((int*) * (int*) (&b) +0); // base::f () (Fun) * ((int*) * (int*) (&B) +1); // base::g () (Fun) * ((int*) * (int*) (&B) +2); // base::h ()
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 Terminator "/0" of the string, which marks the end of the virtual function table. The value of this end flag is different under different compilers. Under winxp+vs2003 , this value is NULL. And in Ubuntu 7.10 + Linux 2.6.22 + GCC 4.1.3 , this value is if 1, indicating there is also the next virtual function table, if the value is 0, 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:
class Public base{ void F1 ();
void
void
}
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.
General inheritance (with virtual function overrides)
Overriding the virtual function of the parent class is a very obvious thing, otherwise the virtual function becomes meaningless.
Let's take a look at what it looks like if a virtual function in a subclass overloads the virtual function of the parent class. Let's say we have an inheritance relationship like the one below.
class Public base{ virtualvoid f (); What happens when you don't have virtual? The F1 function of the parent class is also overridden, and the address is in the same virtual function table, but cannot be overridden by derive subclasses.
virtualvoid
virtualvoid H1 ();}
In order to let you see the effect after the inheritance, in the design of this class, I only covered a function of the parent class:f (). Then, for instances of derived classes, the virtual function table will look like this:
We can see the following points from the table,
1) the covered f () function is placed in the location of the original parent virtual function in the virtual table.
2) functions that are not covered are still.
In this way, we can see that for the following program,
New Derive (); b->f ();
The position of F () of the virtual function table in memory referred to by B has been replaced by the address of the derive::f () function, so when the actual call occurs, it is derive::f () has been called. This enables polymorphism.
Multiple inheritance (no virtual function overrides)
Next, let's look at the case in multiple inheritance, assuming that there is an inheritance relationship for one of the following classes. Note: The subclass does not have a function that overrides the parent class.
classbase1{Virtual voidf (); Virtual voidg (); Virtual voidh ();};classbase2{Virtual voidf (); Virtual voidg (); Virtual voidh ();};classbase3{Virtual voidf (); Virtual voidg (); Virtual voidh ();};classDerive: PublicBase1, public Base2, public base3{Virtual voidF1 (); Virtual voidG1 ();};
For a virtual function table in a subclass instance, the following looks like this:
We can see:
1) Each parent class has its own virtual table.
2) The member function of the subclass is placed in the table of the first parent class. (The so-called first parent class is judged in order of declaration)
This is done in order to resolve pointers to different parent class types to the same child class instance, and to invoke the actual function.
multiple inheritance (with virtual function overrides)
Let's take a look at the case where virtual function overrides occur.
, we overwrite the f () function of the parent class in the subclass.
classbase1{Virtual voidf (); Virtual voidg (); Virtual voidh ();};classbase2{Virtual voidf (); Virtual voidg (); Virtual voidh ();};classbase3{Virtual voidf (); Virtual voidg (); Virtual voidh ();};classDerive: PublicBase1, public Base2, public base3{Virtual voidf ();//overriding the parent class method Virtual voidG1 ();};
The following is a diagram of a virtual function table in a subclass instance:
As we can see, the position of F () in the three parent virtual function table is replaced by the function pointer of the subclass. This allows us to refer to the subclass as a parent of a static type and call F () of the subclass . such as:
*B1 = &*b2 = &*b3 = &d;b1//derive::f ()//Derive :: F ()//derive::f () B1//base1::g ()// Base2::g ()//base3::g ()
C + + virtual function table parsing