C++ static polymorphism and dynamic polymorphism

Static polymorphism (compile time/early binding)
function overloading

class A
{
   
    
    
public:
    void do(int a);
    void do(int a, int b);
};

Dynamic polymorphism (runtime/late binding)

•Virtual function: Use virtual to decorate a member function to make it a virtual function
Note:
• Ordinary functions (non-class member functions) cannot be virtual functions
• Static functions (static) cannot be virtual functions
• Constructors cannot be virtual functions (because in When the constructor is called, the virtual table pointer is not in the memory space of the object, and the virtual table pointer must be formed after the constructor call is completed)
• Inline functions cannot be virtual functions that exhibit polymorphism. For explanation, see: virtual Can a function (virtual) be inline?
Dynamic polymorphic use

 

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/113103162