1#include <iostream>2 using namespacestd;3 classbase{4 Public:5 voidm ()6 {7cout <<"it ' s Base ' s M ()"<<Endl;8 }9 Virtual voidf ()Ten { Onecout <<"It's Base ' s f ()"<<Endl; A } - }; - classSub: PublicBase the { - voidm () - { -cout <<"It's Sub ' s M ()"<<Endl; + } - voidf () + { Acout <<"It's Sub ' s f ()"<<Endl; at } - }; - voidMain () - { -base*Base=NewSub; - Base-f (); in Base-m (); -}
The above is based on base class, and sub derives base, while two functions f () are replicated, M ()
When the main function main uses a base-type pointer to a sub-type object, and then calls F () and M () with the base pointer, then the problem comes.
By default, a pointer to the base class invokes the method of the current type, that is, Base::f (), Base::m (), then the statements in the base method should be output
However, the results are output
Why does it have this reason?
Because the parent class's F () is a virtual function, when the base class pointer invokes its derived class object, the corresponding method of the Polymorphic object is called by default, explaining why Base* Base->f () called the F () method of the derived class sub.
Thus we can know that virtual function virtual is mainly to solve the problem of polymorphism, because after a base class method is defined, its derived class object will implement different overrides for this method, that is, polymorphism, if you use the pointer of the base class to point to the polymorphic subclasses of the base class at this time, Then you need to use virtual to modify the base class method so that you can call the method of the derived class you need and avoid calling this method of the base class.
Virtual is a very important keyword in the C + + oo mechanism. A function that adds the virtual keyword to the class base is a dummy function (such as a function print), so you can override the base class virtual function by overriding the virtual function in the derived class derived of base. When the pointer point of base class base points to an object of the derived class derived, the call to the print function of a is actually called the print function of derived instead of base. This is the embodiment of polymorphism in object-oriented.
There is also a virtual function destructor, which is applied in a problem where the base class is an abstract class, and the base class pointer points to the derived class and wants to delete the memory that the base class pointer points to
Let's look at the first case.
1De<iostream>2 using namespacestd;3 classbase{4 Public:5 Base ()6 {7 }8~Base ()9 {Tencout <<"Base has deleted"<<Endl; One } A }; - classSub: PublicBase - { the Public: - Sub () - { - } +~Sub () - { +cout <<"Sub has delete"<<Endl; A } at }; - voidMain () - { -base*Base=NewSub; - Delete Base; - Base=NULL; in}
See Running results
At this point, the base class base pointer to the operation of the inherited class sub, after the delete base* pointer, we can get only the destructor of the base class is called, but the destructor of the derived class is not called, which is equivalent to the memory is deleted only a half, and half of the memory is deleted, resulting in a memory leak, At this point, you need to use a virtual destructor to reach the destructor of the derived class to remove the memory space, that is, the following situation:
1#include <iostream>2 using namespacestd;3 classbase{4 Public:5 Base ()6 {7 }8 Virtual~Base ()9 {Tencout <<"Base has deleted"<<Endl; One } A }; - classSub: PublicBase - { the Public: - Sub () - { - } +~Sub () - { +cout <<"Sub has delete"<<Endl; A } at }; - voidMain () - { -base*Base=NewSub; - Delete Base; - Base=NULL; in}
The result of the operation is as shown
At this point, we can see that after the base class destructor has been added to virtual function, the destructor of the base class and the derived class is successfully called, and the base class pointer points to the memory's full release.
The virtual destructor works by: The destructor of the bottommost derived class is called first, and then the destructor of each base class is called.
It is important to note, however, that when a class does not have a virtual function, the class is not treated as a base class, and defining a virtual destructor only increases the memory overhead because a virtual function list needs to be opened for storage.
The above is only a small summary of the individual, if there are errors, welcome to correct!
C + + virtual virtual function