Summary of virtual functions

xiaoxiao2021-03-05  19

1. Definition of virtual functions

The virtual function is introduced to better implement the polymorphism of OO ideas, that is, when the parent class pointer points to different objects (belonging to subcategory or parent class), the call will be the version defined by the object belonging to the class. . That is to say, the same statement produces different effects in different environments.

Here is the verification code:

#include class myparentsclass {public: myparentsclass () {cout << "myparentsclass :: myparentsclass" << Endl;} void Testfun () {cout << "myparentsclass :: testfun << Endl; fun }

Virtual void fun () {cout << "myparentsclass :: fun () << end1;} ~ myparentsclass () {cout <<" myparentsclass :: ~ myparentsclass << Endl;

}; class myclass: public myparentsclass {public: myclass () {cout << "myclass :: myclass" << Endl;} void fun () {cout << "Myclass :: fun ()" << endl;} ~ Myclass () {cout << "myclass :: ~ myclass" << endl;}

}

Class mysonclass: public myclass {public: mysonclass () {cout << "mysonclass :: mysonclass << Endl;} void fun () {cout <<" mysonclass :: fun () << endl;} ~ mysonclass ) {Cout << "mysonclass :: ~ mysonclass" << Endl;}

}

INT main () {myparentsclass * pclass = null; myclass * pme = null;

MyParentsclass myparents; myclass me; mysonclass myson; // PME = & myparents; // error, subtempic pointer does not point to parent class object PCLASS = & myparents; pclass-> fun (); pclass = & me; pclass-> fun (); PCLASS = & myson; pclass-> fun ();

Return 0;}

When is it suitable to define a function to be a virtual function?

The above example concludes that the function will be rewritten in the subclass.

2. Delivery of virtual functions:

The virtual function is only transmitted down and does not generate an interrupt, that is, the virtual characteristics of the subclass of the subclass of the subclass will not affect the subclass of the subclass because the subclass is not defined in the subclass.

First verify the downward transfer

Add the following statement in the main function: ........

PME = & myson;

PME-> fun (); // has a virtual characteristic, called the mysonclass's FUN method

.....

Then verify that it will not be interrupted

Remove fun () in myclass definition. And define a subclass again:

Class mygrandsonclass: pulblic mysonclass {public: void fun () {cout << "MyGrandsonclass :: fun ()" << Endl;}};

Add the following statement in the main function:

.........

Mysonclass * pson = null; MyGrandsonclass mygrandson; pson = & mygrandson; pson-> fun (); // can see the characteristics of the virtual function does not disappear

...........

Finally, verify that the virtual function does not have the characteristics of upward:

Remove the modifier Virtual in MyParentsclass, then add Virtual in Myclass or Fun () in MySonclass

Observation results.

Then add the following statement in the main function:

PME = & myson; // only plus Virtual in front of Fun () in mysonclass

PME-> Fun ();

Observation results.

3. Other applications for virtual functions

Add the following code at the main function:

..............

PCLASS-> Fun (); pclass-> testfun (); pclass = & me; pclass-> fun (); pclass-> testfun (); pclass = & myson; pclass-> fun (); pclass-> Testfun ();

.......................

Observation results can be found:

The testfun () method also calls different FUN () functions in the direction of pointer. And when you remove the Virtual in front of Fun (), the result will be different

This draws the following conclusions:

Void testfun () {cout << "myparentsclass :: testfun << Endl; fun (); // This place default is actually this-> fun () // instead of myparentsclass :: fun ()}

PS: Will help for understanding the in-depth of the MFC

Also: Add the following statement in the main function:

myparentsclass * pparents = new myclass ();

DELETE PPARENTS;

PParents = NULL;

Observation results.

You can find delete pParents; there is no destructor of MyParentSClass. If the patterned function is responsible for the recycling of some dynamically assigned memory, this statement will cause serious consequences (pointer suspension).

Workaround: State the destructor of myParentsClass as a virtual function.

Of course, if you don't plan to redefine the designer of this class (subclass ....) in subclasses of subclasses or subclasses.

转载请注明原文地址:https://www.9cbs.com/read-37597.html

New Post(0)