Understanding and usage of virtual (virtual function) in C++

In object-oriented C++ language, virtual function (virtual function) is a very important concept.

What is a virtual function:

  A virtual function refers to a member function in a class that you want to overload. When you use a   base class pointer or reference to    point to an inherited class object, when calling a virtual function, the version of the inherited class is actually called.

#include <iostream>
using  namespace std;

class Parent
{    
 public:
     
     char data[20];
     void Function1();    
     virtual void Function2();   // 这里声明Function2是虚函数
     
 }parent;
 
 void Parent::Function1()
 {
     printf("This is parent,function1\n");
 }
 
 void Parent::Function2()
 {
     printf("This is parent,function2\n");
 }
 
 class Child:public Parent
 {
     void Function1();
     void Function2();
     
 } child;
 
 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;       // 定义一个基类指针
     if(_getch()=='c')    // 如果输入一个小写字母c    
         p=&child;        // 指向继承类对象
     else    
         p=&parent;       // 否则指向基类对象
     p->Function1();     // 这里在编译时会直接给出Parent::Function1()的入口地址。    
     p->Function2();     // 注意这里,执行的是哪一个Function2?
     return 0;
    
 }
Compile and run with any version of Visual C++ or Borland C++, input a lowercase letter c, and get the following result:

1 This is parent,function1
2 This is child,function2
Why is there the result of the first row?
Because we call the function Function1() with a pointer of the Parent class, although the pointer actually points to an object of the Child class, the compiler cannot know this fact (the program can only judge based on the user's input until it is running) . The object pointed to by the pointer ), it can only be understood and compiled as calling the function of the Parent class, so we see the result of the first line.

So what happened to the result of the second row? We noticed that the Function2() function is decorated with the virtual keyword in the base class, that is, it is a virtual function.

The most critical feature of virtual functions is "dynamic binding", which can determine the object pointed to by the pointer at runtime and automatically call the corresponding function.

If we arbitrarily input a non-c character when running the above program, the result is as follows:

1 This is parent,function1
2 This is parent,function2
Please pay attention to the second line, its result has changed. Only one Function2() function is called in the program, but it can automatically decide whether to call Function2 in the base class or Function2 in the inherited class according to the user's input. This is the function of the virtual function.
PS: Be sure to pay attention to the difference between "static binding" and "dynamic binding"; for me, if I don't test it in VS2008, I can
When I input "c" on the keyboard, I think that because of the code p=&child;, I think the result is:

1 This is child,function1
2 This is child,function2

But the result is:

1 This is parent,function1
2 This is child,function2
Because although this pointer actually points to an object of the Child class, the compiler cannot know this fact, it can only understand and compile it by calling the function of the Parent class, so we see the result of the first line.
The function2 of the subclass is called in the second line, which is entirely because of the function of virtual. Virtual realizes dynamic binding, which can judge the object pointed to by the pointer at runtime and automatically call the corresponding function.
1 p=&parent; //In this sentence, the pointer obviously points to the parent class, so the method of the parent class must be called

Guess you like

Origin blog.csdn.net/qq_38315190/article/details/100553838