Backward
---- virtual function
Author: HolyFire
Say a person, it is usually said soft in the ear. People say what to forget, others say to east, he will go east, someone says to west, he immediately rushed back. However, the benefits are obvious, this person is specially observed, if you don't know what you have to do, or if you don't know what to do, then ask him to wait, when you get your mind, tell him your request, Let him take it. There is a lot of reality, and the secretary needs to record, write reports, arrange itinets, contact customers, etc., and these work is done in your instruction. If you have such an obedient code when programming, it is not a dream, which is fully realized.
When our elementary school, we learned to solve the equation. The concept mentioned in it has an unknown, and he uses a number that can be used for any number of symbols, and then obtains the required value by the calculation rules, with a programming angle Looking, he adds an indirect layer, we temporarily do not deal with the value, but to deal with a symbol related to the value, if the value is the value, we can use the member variable to handle, if the change is Calculation rules. Remember to do micro points, y = f (x). This f (x) is variable, let our solution generally be used in a variety of possible F (x), then provide an indirect layer to connect different functions, this is too simple in C , functions The pointer can be done.
The role of the pointer is to save a certain type of object in the memory, changing the value of the pointer, allowing him to point to different objects. (The image of the image can make us more easily understand the intention of the pointer, the pointer itself is also a type)
Implementing the virtual function in C is using a function pointer, the compiler hides a function pointer array (because the member function may have a lot) called virtual function table.
Class a {
//...data MEMBERS
PUBLIC:
Virtual func1 ();
Virtual func2 ();
Virtual func3 ();
//...func Members
}
There are three virtual functions in this class.
They are in some place in memory
Address1 [A :: Func1]
Address2 [a :: func2]
Address3 [A :: Func3]
Instantiate a class A object A
A a a;
Get A structure is like this
[
Virtual function table
[Func Pointer1] Address1
[Func Pointer2] Address2
[Func Pointer3] Address3
]
[
Data MEMBERS
...
]
When using a member function func1, it is actually the location of [A :: Func1] in [A :: Func1] in [Func Pointer1], then call it, obviously the step of looking for function locations here, so use virtual The function will reduce the execution efficiency, the hits between efficiency and flexibility, will see your own requirements.
Further, when inheriting a class B from a, what is the situation?
Class B: Public a {
//...data MEMBERS
PUBLIC:
Virtual func1 ();
Virtual func2 ();
Virtual func3 ();
//...func Members
}
There are three virtual functions in this class.
They are in some place in memory
Address4 [B :: Func1]
Address5 [B :: Func2] address6 [b :: func3]
An object of instantiation B
B B;
Getting B structure is like this
[
A virtual function table
[Func Pointer1] Address1
[Func Pointer2] Address2
[Func Pointer3] Address3
]
[
A Data MEMBERS
...
]
[
B virtual function table
[Func Pointer1] Address4
[Func Pointer2] Address5
[Func Pointer3] Address6
]
[
B Data MEMBERS
...
]
In order to correctly implement B-> FUNC1, [b :: func1] at Address4 must be able to know the virtual function table to go to B, huh, good way should not only use it once, smart readers will know Add a pointer to operate different virtual functions, right, here C hide a virtual function table pointer to access different virtual functions, when Class B is created, this pointer points to B's virtual function table .
For another, if you inherit class C, you can point to the virtual function table of the virtual function table pointer.
Here, the trick of the virtual function will be dismantled, and the pointer in C is a good thing. If you use it, you can work hard.
We use C to see the effect of virtual functions
#include
#include
Using namespace std;
Class normala {
PUBLIC:
Void Dosomething (void) {cout << "this is class normala." << endl;}
}
Class Normalb: Public NormalA {
PUBLIC:
Void Dosomething (void) {cout << "this is class normalb." << endl;}
}
Class Normalc: Public Normalb {
PUBLIC:
Void Dosomething (void) {cout << "this is class normalc." << endl;}
}
Class Virtuala {
PUBLIC:
Virtual void dosomething (void) {cout << "this is class virtuala." << endl;}
}
Class Virtualb: Public Virtuala {
PUBLIC:
Void Dosomething (void) {cout << "this is class virtualb." << endl;}
}
Class Virtualc: Public VirtualB {
PUBLIC:
Void Dosomething (void) {cout << "this is class virtualc." << endl;}
}
int main ()
{
Normala NA;
Normalb NB;
Normalc NC;
Virtuala va;
Virtualb VB;
Virtualc vc;
Normala * PNA;
Virtuala * pva;
Na.DOSMETHING ();
nb.dosomething (); nc.dosomething ();
Va.dosomething ();
vb.dosomething ();
vc.dosomething ();
PNA = & NA;
PNA-> DOSMETHING ();
PNA = & nb;
PNA-> DOSMETHING ();
PNA = & NC;
PNA-> DOSMETHING ();
PVA = & VA;
PVA-> DOSMETHING ();
PVA = & VB;
PVA-> DOSMETHING ();
PVA = & VC;
PVA-> DOSMETHING ();
cin.get ();
Return 0;
}
turn out
This is class normala. // call NORMALA :: DOSMETHING ();
This is class normalb. // is called Normalb :: DOSMETHING ();
This is class normalc. // is called Normalc :: DOSMETHING ();
This is class virtuala. // Calling Virtuala :: DOSMETHING ();
This is class virtualb. // Calls Virtualb :: DOSMETHING ();
This is class virtualc. // is called Virtualc :: DOSMETHING ();
This is class normala. // call Normala :: DOSMETHING (); no virtual function table, Normala * specified type
This is class normala. // call Normala :: DOSMETHING (); no virtual function table, Normala * specified type
This is class normala. // call Normala :: DOSMETHING (); no virtual function table, Normala * specified type
This is class virtuala. // Calling Virtuala :: DOSMETHING (); virtual function table pointer pointing to Virtuala
This is class virtualb. // Calls Virtualb :: DOSMETHING (); virtual function table pointer points to Virtualb
This is class virtualc. // Calling Virtualc :: DOSMETHING (); virtual function table pointer pointing to Virtualc
The virtual function seems to have some common works, but the function overload can determine the function we need to use during compilation, which is predictable; the virtual function can determine the specific function at runtime, is it It is predicted that there is a dedicated term for virtual functions ---- evening binding, using virtual functions, this method is called function coverage.
2001/8/20
Ding Ning