The following points are from the introduction to MFC:
Which function is called depends on the original type of the pointer, and the object actually referred to by the pointer is irrelevant.
1. If you direct a "base class Pointer" to a "derived class", you can only call the functions defined by the base class through this pointer.
2. If you direct a "pointer to a derived class" to a "base class Object", you must first perform the explicit Transformation Operation (explicit cast)/(display forced type conversion ). This practice is dangerous and does not conform to real life experience. It will also bring confusions to programmers in programming.
3. if both the base class and the derived class define "member functions with the same name", which function is called when a member function is called through an object pointer, the pointer must be of the original type, rather than the type of the object actually referred to by the pointer. This is equivalent to the 1st point.
The purpose of a virtual function is to: "If you point a base class pointer to an object of A derived class, with this pointer, you can only call the member functions defined by the base class. "This rule is the opposite design. It's also a bit of anti-inheritance. Polymorphism.
From the operation type definition, what is a virtual function? If you expect a derived class to redefine a member function, you can set this function to virtual in the base class. MFC has two very important virtual functions: the serialize function related to document and the ondraw function related to view. You should rewrite these two virtual functions in your CMyDoc and CMyView.
Polymorphism)
Different functions are called using the same instruction pointer. This property is called Polymorphism, meaning "the ability to assume into forms" (Polymorphism ). The compiler cannot determine which derived class function is called by the base class pointer during the compilation period. It must be determined during the execution period. This is called binding late binding or dynamically binding dynamic binding later. As for the C function or the non-virtual function of C ++, it is converted into a fixed address call during the compilation period. This is called the early binding of early binding or static binding of static binding.
Pure virtual functions
The value of the pure virtual function is "= 0 ". Pure virtual functions do not need to define their actual operations. They exist only to be redefined in the derived class, but to provide a multi-state interface. As long as it is a class with pure virtual functions, it is an abstract class and cannot be instantiated (instantiate). That is to say, you cannot generate an object based on it.
Abstract classes.
If the abstract class is inherited and the pure virtual function in the base class is not rewritten in the derived class, the derived class is still an abstract class until the pure virtual function is actually operated.
The virtual function is still a virtual function, and the virtual keyword can be omitted.