Object-oriented
C
In the language, the virtual function (
Virtual function
) Is a very important concept. Because it fully reflects the two major characteristics of inheritance and polymorphisms in object-oriented ideas.
C
The language is widely used in the language. For example in Microsoft
MFC
In the library, you will find that many functions have
Virtual
Keywords, that is, they are virtual functions. No wonder someone even called the virtual function is
C
The essence of the language.
So, what is virtual function, let's take a look at Microsoft's explanation:
The virtual function refers to a member function you want to overload in a class. When you use a base class pointer or reference to a inherited class object, you call a virtual function, actually call the inheritance version.
- Excerpt from MSDN
This definition is not very clear. An example is also given in MSDN, but its examples are not well explained. We write such an example:
#include "stdio.h"
#include "conoe.h"
Class Parent
{
PUBLIC:
Char Data [20];
Void function1 ();
Virtual void function2 (); // Here, FUNCTION2 is virtual function
} PARENT;
Void Parent :: function1 ()
{
Printf ("This Is Parent, Function1 / N");
}
Void Parent :: function2 ()
{
Printf ("This Is Parent, Function2 / N");
}
Class Child: Public Parent: Public Parent: PUBLIC PARENT
{
Void function1 ();
Void function2 ();
}
Void Child :: function1 ()
{
Printf ("this is child, function1 / n");
}
Void Child :: function2 ()
{
Printf ("this is child, function2 / n");
}
Int main (int Argc, char * argv [])
{
PARENT * P; / / Define a base class pointer
IF (_Getch () == 'c') // If you enter a lowercase letter C
P = & child; // point to inheritance object
Else
P = & pent; / / Otherwise pointing to the base class object
P-> Function1 (); // Here is directly sent to Parent :: function1 ()
Entrance address.
P-> function2 (); // Note which FUNCTION2 is executed?
Return 0;
}
Compile and run with any version of Visual C or Borland C , enter a lowercase letter C to get the following result:
This is parent, function1
This is child, function2
Why does there be the result of the first line? Because we use a Parent class to call the function fuction1 (), although this pointer points to the Child class object, the compiler cannot know this fact (until running, the program can be judged according to the user's input Objects pointed to the pointer), which can only be understood and compiled according to the function of calling the Parent class, so we see the results of the first line.
So what is the result of the second line? We noticed that the function2 () function is modified by the Virtual keyword in the base class, that is, it is a virtual function. The most critical feature of virtual functions is "Dynamic Board", which can determine the object pointing to the pointer at runtime and automatically call the corresponding function. If we enter a non-C character when we run the above program, the result is as follows: this is parent, function1
This is parent, function2
Please take note of the second line, and its results have changed. The program is only called only a function2 () function, but can automatically determine the FUNCTION2 in the base class in the base class according to the user's input, which is the function of the virtual function. We know that in the MFC, many classes need you inherited, and their member functions must be overloaded, such as writing the most common CView :: OnDRAW (CDC *) function written by MFC applications, and must be overloaded. Define it as a virtual function (in fact, on the mfc ondraw is not only virtual function, but also a pure virtual function), you can ensure that the time call is the onDraw written by the user yourself. Important use of virtual functions can be seen here.