Learning C + + inevitably encounter virtual function problems, the following problems in the early learning more or less there will be some doubts, so it summed it down.
1. Why can't static member functions and constructors be defined as virtual functions? because the static member function is a shared resource, it is actually a "restricted by class domain qualifier" normal function, there is no this pointer, do not need the object can be called, and the virtual function is a member of the real deal function,
The invocation relies on the created instance (the this pointer at compile time to give the address of the instance to the member function) so one dependent object, one no, the two are contradictory. for the constructor, because the instance is not created when the instance is constructed by running the constructor, and the virtual function is built on the basis of the object building, it is not feasible to define the constructor as a virtual function. 2. Why not call the virtual function in a constructor or destructor?
It is straightforward to say that because the virtual function of the call does not reach the effect of virtual function, and the realization of virtual function has a price, the result is thankless.
Consider one of the following examples
classbase{ Public: Base () {vfunc (); } Virtual voidVfunc () {cout<<"Base::vfunc ()"<<Endl; }};classDerive: Publicbase{ Public: Derive (): Base (), _pdata (New int(2) ) {vfunc (); } Virtual voidVfunc () {cout<<"Derive::vfunc ()"<<*_pData<<Endl; } ~derive () {Delete_pdata;}Private: int*_pdata;};voidTest () {Derive D;}Example
The result is this:
We all know that when we construct the Subclass object D, we first construct the base class component in D, that is to call the base class constructor, but now the virtual function here seems to be a rewrite, so how is the output not so?
2 2
But the answer is obviously wrong!! The previous ' 2 ' is not possible, it's not yet original.
Now that the problem is found, the effectivec++ point of view is that " during base class construction, the virtual function is not a virtual function", where VFUNC () does not achieve the effect of the virtual function.
The reason is that the base class component is constructed when Vfunc () of the derived class is not registered in the virtual table in D, and only the Vfunc () of the base class is called, so this kind of virtual function call does not fall into the subclass. to take a step back, if you descend into a subclass, it will also appear in the above type of access subclass of uninitialized member _pdata, causing the program to crash. So why do you call it in the constructor? There is no virtual function effect, also affect efficiency.
The same is true for destructors. The derived class part is first refactored and then the base class part, but only the function of the base class itself can be called at this time. If the derived class destructor is executed again, the derived class member variable (such as _pdata) in the object renders an undefined value, leading to an unknown behavior. (If you change the base class to a pure virtual function and call it in construction and destruction, the compiler will not be "restless" at this time)
Therefore, do not adjust the virtual function in the structure, destructors, it does not have the desired result (polymorphic), even if there is also a "night to debug the assembly string of direct ticket."
3. When do you want to declare a destructor for a base class as a virtual function?
A: an inherited class object needs to be destroyed as a virtual function when it is deleted through a base class pointer.
The compiler always calls the class member function according to the type. However, a pointer to a derived class can be safely converted to a pointer to a base class. When you delete a pointer to a base class, C + +, regardless of whether the pointer points to a base class object or a derived class object, calls the base class's destructor instead of the derived class, so that the component of the inheriting class is not destroyed, causing the local destruction of the object, resulting in a resource leak.
Therefore, it is recommended to declare destructors as virtual functions. This enables polymorphism to avoid memory leaks
Note:
Once a function in the ① base class is declared as a virtual function, the subclasses form the same function to keep the virtual function attribute, regardless of whether the subclass is added with virtual
② destructor is very special, because the base class and subclass of the destruction of their underlying is actually the same name (destructor), so it will constitute an overlay
Although virtual functions have benefits, C + + does not use virtual destructors directly as default values . The reason is that the cost of the virtual function table and the compatibility with the C language type. An object with a virtual function always contains an implicit virtual function table pointer member at the beginning of the position. Therefore, if the class we are designing does not involve inheritance relationships, it is not necessary to make the destructor a virtual function.
A small set of c++--virtual function problems