Virtual functions in the 1.c++
The function of virtual function in C + + is mainly to realize the mechanism of polymorphism. About polymorphism, in short, is to use the pointer of the parent type to an instance of its subclass, and then call the member function of the actual subclass through the pointer to the parent class. This technique allows the pointer of the parent class to have "multiple forms," which is a generic technique. The so-called generic technology, plainly is to try to use immutable code to implement the variable algorithm. For example: template technology, RTTI technology, virtual function technology, or try to do at compile-time resolution, or try to do run-time resolution.
People who know C + + should know that virtual functions (virtual function) are implemented by a virtual function table (virtual table) and a pointer to a virtual function table (vptr). virtual function table, referred to as VTBL, virtual function table table plays an important role in realizing polymorphism. In this table, we mainly save the address of a virtual function in a class, which solves the problem of inheritance and overlay, so that its content can actually reflect the actual function. Each instance of a class that contains a virtual function contains a cptr pointer to the first address of the virtual function table. We can find the virtual function to access through this pointer, the completion of virtual function calls mainly include: to find the first address of the virtual function table (vptr), through the Cptr find to use virtual function address, call virtual function. So using virtual functions, you always have to consider the problem of efficiency, in fact, in order to improve efficiency, C + + compiler is to ensure that the virtual function table pointer exists in the first position in the object instance, this is to ensure that the virtual function table has the highest performance, which means that we get through the address of the object instance of the virtual function table, You can then iterate through the table to find the address of the virtual function, and then call the corresponding function. Consider the following code:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class Base
{
Public
virtual void F () {cout << "base::f" << Endl;}
virtual void g () {cout << "base::g" << Endl;}
virtual void H () {cout << "base::h" << Endl;}
};
typedef void (*fun) (void);
int main ()
{
Base b;
Fun pfun = NULL;
cout << "virtual function table Address:" << (int*) (&b) << Endl;
cout << "virtual function table-First function address:" << (int*) * (int*) (&b) << Endl;
Pfun = (Fun) * (int*) * (int*) (&b));
Pfun ();
return 0;
}
With this example, you can see that by forcing the &b into int *, you get the address of the virtual function table (vptr), and then you can get the address of the first virtual function again, which is base::f (), which is validated in the above program (putting int* Force to a function pointer). With this example, we can see that if you want to invoke Base::g () and Base::h (), the code is as follows:
Copy Code code as follows:
(Fun) * ((int*) * (int*) (&b) +0); Base::f ()
(Fun) * ((int*) * (int*) (&b) +1); Base::g ()
(Fun) * ((int*) * (int*) (&b) +2); Base::h ()
You can see how the graph of the virtual function table is drawn:
As we all know, polymorphism is implemented through inheritance, so we have to talk about the problem of the inheritance of virtual function. Inheritance involves the covering of virtual functions, and what is the connection between virtual functions and polymorphism that are not actually covered? Here we discuss what a virtual function table is covered with, assuming that the following inheritance relationship exists:
Look at what the virtual function represents:
It can be found that the base::f () is overwritten so that if the derive instance is assigned to a base-type base pointer pbase through PBASE->F (), then the access to the F () in the subclass is the completion of polymorphism. So what exactly is the content in the virtual function table? You can look at the following four words will understand!
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 subclass.
3. The covered f () function is placed in the position of the original parent class virtual function in the virtual table.
4. Functions that are not covered are still.
2. Realize polymorphism with virtual function
Look at the following polymorphic code:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class Base
{
Public
virtual void Print ()
{
cout<< "Base::P rint ()" <<endl;
}
};
Class Derive:public Base
{
Public
virtual void Print ()
{
cout<< "Derive::P rint ()" <<endl;
}
};
int main ()
{
Derive derive;
Base *pbase = &derive;
Pbase->print ();
return 0;
}
Polymorphic Code
To implement the code for a virtual function, be sure to remember that the pointer to the base class points to the address of the object of the subclass. First try to understand the use of virtual functions to achieve the principle of polymorphism, if you do not understand why virtual functions can achieve polymorphism, and why this implementation of polymorphism, the Internet and then search for relevant information!!!