Every time you see the C ++ class, declare a virtual function, especially a virtual fictitious function. Do you know what it means? You will certainly not hesitate to answer: Isn't it polymorphism... Are you sure you want to perform specific actions during the running... It is completely correct, but here I want to talk about more than these things.
Some classes require virtual functions, while others do not. This is why. Generally, if the class you see has a virtual destructor, there should be at least one virtual function in the class .. Why ?? If no other virtual functions are used in our class, it is unnecessary to create this function and the class object size will be increased .. Let's talk about these purely theoretical things .. The following is an example for verification ..
1:
class A{public:A(){};//virtual ~A(){};~A();};void main(){A a;cout<<sizeof(a)<<endl;}
The result is 1. This 1 should be added by the compiler itself .. Even if you do not write anything in the class, it is also 1; for example;
class A{};void main(){A a;cout<<sizeof(a)<<endl;}
If you declare the Destructor as a virtual function .. For example:
2:
Class
{
Public:
A (){};
Virtual ~ A (){};
//~ A ();
};
Void main ()
{
A;
Cout <sizeof (a) <Endl;
}
The result is 4. Not to mention why ..
Next, let's talk about the basics of virtual functions (polymorphism). Here is an example:
# Include <iostream> # include <string> using namespace STD; class base {public: Base (); Virtual ~ Base (); Virtual void test (); Private: int count;}; base: Base () {cout <"base part created" <Endl;} base: :~ Base () {cout <"base part is destroyed" <Endl;} void base: Test () {cout <"base test" <Endl ;} class derive1: public base {public: derive1 (); Virtual ~ Derive1 (); void test () ;}; derive1: derive1 () {cout <"subclass part created" <Endl;} derive1 ::~ Derive1 () {cout <"The subclass part is destroyed" <Endl;} void derive1: Test () {cout <"derive1 test" <Endl ;} void main () {base * D1 = new derive1 (); D1-> test (); Delete D1 ;}
It can be seen that when a parent class pointer is declared and directed to an object of a subclass, the constructor of the parent class is called first when the sub-object is created, then there is your own constructor. When a virtual function test () is called through the parent class pointer, it actually calls the test () function of the subclass. Why, is there certainly any information for it to do this .. This information must be provided by the subclass object .. This information is the virtual function pointer (vptr), which points to a virtual function table (vtbl ), this virtual function table actually contains the name (function pointer) of all the virtual functions of this class. Each class only contains the virtual function pointer and some member variables. This explains why the above is 1 and why it is 4 ..
On win32 machines, each pointer is 4 bytes. The size of each class depends on two parts. One is a member variable and the other is a virtual function pointer and there is only one. In example 1, because no member variable exists, there is a virtual function-the destructor. At this time, there must be a virtual function pointer, so it is 4 .. In fact, the essence of polymorphism is also clearly stated just now, that is, the virtual function pointer of the sub-object gives this information, and the parent class pointer knows which function to execute ..
Last question: Why should the Destructor be declared as a virtual function? (When at least one function is a virtual function)
We can also see from the result just now that when we delete the pointer, the structure will be generated, and this process is performed in the sequence from the subclass to the parent class. If the Destructor is not a virtual function, the parent class pointer does not know how to execute the destructor of the subclass .. This will not release the part of the sub-Object Memory, resulting in Memory leakage .. For example: (Here we only modify the previous Code and remove the virtual in the parent class ):
# Include <iostream> # include <string> using namespace STD; class base {public: Base ();~ Base (); Virtual void test (); Private: int count;}; base: Base () {cout <"base part created" <Endl;} base: :~ Base () {cout <"base part is destroyed" <Endl;} void base: Test () {cout <"base test" <Endl ;} class derive1: public base {public: derive1 ();~ Derive1 (); void test () ;}; derive1: derive1 () {cout <"subclass part created" <Endl;} derive1 ::~ Derive1 () {cout <"The subclass part is destroyed" <Endl;} void derive1: Test () {cout <"derive1 test" <Endl ;} void main () {base * D1 = new derive1 (); D1-> test (); Delete D1 ;}
So when there is at least one virtual function, we should declare its destructor as virtual. (Some people will say where the function in your subclass is a virtual function. I didn't see the virtual function .. In fact, C ++ allows us to do this. It is not necessary to declare the virtual function of the parent class .)
Summary: A class has a virtual function to become a base class. If this is not the case, no function in the parent class must be a virtual function, it will even increase the class size. Polymorphism tells us this .. Once it becomes a base class, it is necessary to declare the Destructor as a virtual function ..
Okay, the content of the virtual function will be over .....