|
Thank you all! I have benefited a lot from it!
Regarding the question of "adding virtual before the destructor", based on your advice and the book, I think if there is nothing wrong in my understanding, it looks like this:
///////////////////////////////////////////////// ////////////////////////////////
First of all, each derived class object has a pointer to the virtual function table. Any virtual function is accessed indirectly through this pointer. The reason for using the virtual function table is because which version of a virtual function is called is running The process (the object pointed to by the pointer at the time of the call) can be determined (dynamic binding).
Compared with virtual functions, the calling mechanism of real functions is much simpler: since each real function has only one version, the calling address can be determined at compile time (static binding)
The destructor can also be called a virtual function through virtual modification. The difference between a virtual destructor and a general virtual function is:
1 "Its redefinition function is the destructor of the derived class, but the same name is not required.
2" After a version of a virtual destructor is called, it is then necessary to call the execution base class version, and so on, until the first version of the virtual destructor that executes the derived sequence is called.
///////////////////////////////////////////////// ////////////////////////////////
The above is quoted from "National Computer Rank Examination Level Two Course-C++ Language Programming"
In other words, the object produced by Base *p=new Derive; is Base.
When virtual is not added before the base class, the destructor of the derived class is not added to the virtual function table. At this time, the destructor is a real function, and there is only one version, which is the object's own ~Base();
When virtual is added before the base class, the destructors of the derived class and the base class are added to the virtual function table. When the destructor is called, the destructor of the version pointed to by the current pointer is called. The next thing: a virtual After the version of the destructor is called, the base class version is then called, and so on, until the first virtual destructor version that executes the derived sequence is called.
5555~ It's not easy~
I want to say something-long live understanding!! Thank you so much!!!
Finally, please help everyone to find out what is wrong with the above ^-^ |
|