A virtual function is associated with polymorphism, and polymorphism is associated with inheritance. So this article is on the inheritance level of the fuss. Without inheritance, nothing is to be talked about. The following is an understanding of the virtual function of C + +. One, what is a virtual function (if you do not know what the virtual function is, but there is an urgent need to know, then you should start from here) simply say, those who are modified by the Virtual keyword member function, is the virtual function. The function of virtual function, the use of professional terminology to explain is to achieve polymorphism (polymorphism), polymorphism is the interface and implementation of the separation, with the image of the language is to achieve a common approach, but because of individual differences and adopt different strategies. Let's look at a simple code class a{public:void print () {cout<< "This is A" <<endl;}}; Class B:public A{public:void print () {cout<< "This is B" <<endl;}}; int main () {///in order to make it easier to distinguish later, my main () code is called main1a A; B b;a.print (); B.print ();} With the print () interface of Class A and class B, we can see that these two classes adopt different strategies depending on the individual differences, and the output is what we expect, respectively, that is a and this is B. But is this really polymorphism? No, polymorphism There is also a key point is everything with a pointer to the base class or reference to manipulate the object. Now change the code at main (). int main () {//main2a A; b b; A * p1=&a; A * P2=&b;p1->print ();p 2->print ();} Run a look at the results, Yo Ah, suddenly looking back, the result is two A. The problem is, P2 clearly points to the object of Class B but is called the print () function of class A, which is not the result we expect, then we need to solve this problem to use the virtual function class a{public:virtual void print () {cout << "This is A" <<ENDL;} Now it's a virtual function.};class b:public a{public:void Print () {cout<< "This is B" <<ENDL;} Here we need to add a keyword to the front VirtuaL? There is no doubt that the member function print () of Class A has become a virtual function, so is the print () of class B a virtual function? The answer is yes, we simply set the member function of the base class to virtual, and the corresponding function of its derived class automatically becomes a virtual function. Therefore, the print () of Class B also becomes a virtual function. If you need to modify the virtual keyword before the corresponding function of the derived class, it is your own problem. Now rerun the MAIN2 code so that the result of the output will be this is a and this is B. Now to digest, I make a simple summary, the pointer to the base class when manipulating its Polymorphic class object, according to the different class object, the corresponding function is called, this function is a virtual function.
From Baidu Onlinewan
Reprint: What is virtual function