C ++ does not use virtual to implement Polymorphism or virtual polymorphism.
 
You can use the member function pointer to implement polymorphism without using virtual.
 
Member function pointer format: return type (A: * pointer name) (parameter table)
 
A is A class type, that is, this pointer is A function pointer to A class member function.
 
For example, int (A: * P) (int, int) is A pointer to "A class member function with two int-type parameters and the return value is int, the pointer name is P.
 
The following code is used:
 
# Include <iostream> using namespace std; class Base; typedef int (Base: * p) (); // defines the member function pointer type class Base {public: p virtual_p; // member function pointer Base () {// initialization member function pointer refers to the Base class test virtual_p = (int (Base: *) () & Base: test ;}~ Base () {} int test () // determines the direction of virtual_p. if it points to the derived class test, the test call of the derived class is returned {if (virtual_p = & Base: test) {cout <"Base" <endl; return 1;} else return (this-> * virtual_p) (); // return the test call of the derived class, the test function }}; class Derived: public Base {public: Derived () {// initialize the member function pointer to the Derived class test virtual_p = (int (Base :: *) () & Derived: test ;}~ Derived () {} int test () {cout <"Derived" <endl; return 2 ;}}; int main () {Base * B; // Base class pointer Derived d; // Base bb of the Derived class object; // Base class Object // Base class test is called when the Base class object is directed to, and test is called when the Base class object is directed to the Derived class object, simulate the dynamic Association of B = & d; cout <B-> test () <endl; B = & bb; cout <B-> test () <endl; return 0 ;} 
The running result is as follows: