Summary: There is a very special stuff in the virtual function, that is, a pure virtual function, the problem about the pure virtual function is also a common topic on BBS. Here I want to make a small discussion, I hope to give beginners. Satisfactory explanation.
First, introduce reasons: 1. In order to facilitate the use of polymorphism, we often need to define virtual functions in the base class. 2, in many cases, the base class itself generates the object is unreasonable. For example, animals can be used as a base class to derive a subclass such as a tiger, peacock, but the animal itself is significantly uncommon. In order to solve the above problems, the concept of pure virtual functions is introduced, and the function is defined as a pure virtual function (method: virtual returntype function () = 0;), the compiler requires that it must be overloaded in the derived class to achieve polymorphism . At the same time, a class containing a pure virtual function is called an abstract class, which cannot generate an object. This solves the above two problems very well.
Second, the pure virtual function is essentially: The class contains a pure virtual function, its VTable table is incomplete, there is a vacancy, so it cannot generate an object (the compiler is absolutely not allowed to call an absence of a function). In its derived class, unless this function is kept, the VTABLE table of this derived class is incomplete, and it cannot generate an object, that is, it is a pure virtual base class.
Third, virtual functions and constructs, destructors: 1, the constructor itself cannot be a virtual function; and the virtual mechanism does not work in the constructor (the virtual function in the constructor only calls its local version). Think about it, use the virtual mechanism in the base class constructor, may call the subclass, at this time, the subclass has not generated, what consequences! ? . 2, the destructor itself is often required to be a virtual function; but the virtual mechanism does not work in the destructor. If the virtual function is used in the class, the destructor must be a virtual function, such as using the virtual mechanism to call Delete, without a virtual destructor, how can Delete's object you want Delete. The virtual machine does not take effect in the destructor, as it may cause the problem of the virtual function that calls the class that has been dropped by Delete.
Fourth, the object slice: When the back map (the subclass is mapped to the parent class), the case where the subclass's VTABLE is completely the VTABLE of the parent class. This is the object slice. Cause: When the introduction is mapped, the interface will narrow, and the compiler is absolutely not allowed to call a possibility that there is no function. Therefore, the entry of the newly born virtual function in the subclass will be forced "cut" in the vTable. Thereby the above is present.
5. The shortcomings of virtual functions use a lot of piles, now talk about the shortcomings, the most important disadvantage of virtual functions is that the implementation efficiency is low, see the realization process of the polymorphism caused by the virtual function, you will experience Among them.