Detailed explanation of C++ virtual functions

virtual function definition

The so-called virtual function is to define an unimplemented function name in the base class. In order to improve the readability of the program, the virtual function is added with the virtual keyword. General format:

class base
{
    
    
public:
 base();
 virtual void test(); //定义的一个虚函数
private:
 char *basePStr;
};

The significance of the existence of virtual functions: to achieve polymorphism in C++.
The core purpose of using virtual functions is to access functions defined in derived classes through base classes.

The above code defines a virtual function of test in the base class, and all the behaviors that can redefine the parent class in its subclasses become overrides, or rewrites.

 
#include<iostream>  
using namespace std;  
  
class A  
{
    
      
public:  
    void foo()  
    {
    
      
        printf("1\n");  
    }  
    virtual void fun()  
    {
    
      
        printf("2\n");  
    }  
};  
class B : public A  
{
    
      
public:  
    void foo()  //隐藏:派生类的函数屏蔽了与其同名的基类函数
    {
    
      
        printf("3\n");  
    }  
    void fun()  //多态、覆盖
    {
    
      
        printf("4\n");  
    }  
};  
int main(void)  
{
    
      
    A a;  
    B b;  
    A *p = &a;  
    p->foo();  //输出1
    p->fun();  //输出2
    p = &b;  
    p->foo();  //取决于指针类型,输出1
    p->fun();  //取决于对象类型,输出4,体现了多态
    return 0;  
}

Common Mistakes with Virtual Functions

unintentional rewrite

An example of unintentional rewriting is as follows. A member function with the same signature as a virtual function of the base class is declared in a derived class, and this virtual function is accidentally rewritten.

class Base {
    
    
public:
    virtual void Show(); // 虚函数
};
 
class Derived : public Base {
    
    
public:
    void Show(); // 无意的重写
};

virtual function signature mismatch

The signature of a function includes: function name, parameter list, const attribute.

Virtual function signature mismatch errors are usually due to differences in the function name, parameter list, or const attributes, resulting in the accidental creation of a new virtual function instead of overriding an existing virtual function.

class Base {
    
    
public:
    virtual void Show(int x); // 虚函数
};
 
class Derived : public Base {
    
    
public:
    virtual void Sh0w(int x); // o 写成了 0,新的虚函数 
    virtual void Show(double x); // 参数列表不一样,新的虚函数 
    virtual void Show(int x) const; // const 属性不一样,新的虚函数 
};

For the above three writing methods, the compiler will not report an error, because it does not know that your purpose is to rewrite the virtual function, but treat it as a new virtual function.

Note: The inheritance of virtual functions is permanent, that is to say, the parent class declares a certain function as a virtual function, and it is still a virtual function in the grandchildren and grandchildren. Dynamic polymorphism can still be achieved: according to the type actually pointed to Choose the method of execution. However, for readability and clear meaning, it is also recommended to add the virtual keyword to derived classes.

Guess you like

Origin blog.csdn.net/qq_32761549/article/details/129982284