Xiao talks about the false argument function, you can first look at the following example: #include
A: When the designer of the base class Mother is not the Virtual output situation: ~ mother () ~ mother () ~ SON () ~ mother () ~ grandson () ~ SON () ~ Mother () first line is delete one The result of the output; the second line is the output result of Delete TWO; third. The four lines are the output of Delete Three; the 5th to 7th line is the fictional function of GrandSon Four. B: When the base class Mother's destructor is Virtual output The situation is: ~ SON () ~ mother () ~ grandson () ~ SON () ~ mother () ~ grandson () ~ SON () ~ mother () ~ grandson () ~ SON () ~ mother () obviously discovery output The result is much safe, and each destructor is called. This is very important for the robustness and security of the program. Second, the example you think of from the above example can see if you use Grandson Four Whether it is derived or the destructor of the base class is called. Why is it created with new, only when the analyte function is Virtual? The analysis is as follows: ONE, Two, Three after New, TWO, Three is a pointer class, and ONE is the nature of SON after the Mother class. One is the first declaration of the Mother class, so when Mother's destructor is not Virtual, One, Two, Three first calls the Mother's destructor. The destruct function of the subclass is not adjusted .. When Virtual, similar For the Override (overwriting). Reference << Thinking In C >> One Paragraph: IF The Pointer Is To The Base Class, The Compiler Can Only Know To Call The Base-Class Version of The Destructor DURING DELETE.SOUND FAMILIAR? This is the same problem that virtual functions were created to solve for the general case.Fortunately, virtual functions work for destructors as the do for destructors as they do for all other functions except constructors following examples:. #include
Class Son: public mother {public: void talk () {cout << "i am your Son";}};