Analysis of base class virtual destructor function

Source: Internet
Author: User

Polymorphism is realized by virtual function table, and dynamic binding is realized by parent class pointer. When a subclass overrides a virtual function of the parent class, it overrides the virtual function in the table of the virtual function in its original position in the table, which may be called "overwrite".

Is that the same as the destructor? The destructor of a base class is often seen as a virtual function in program code to prevent memory leaks caused by the following:

Class A;

Class B:public a{};

......

b *b = new B ();

A *p = (A *) b;

Delete p;

At this point, the system calls only the destructor of a, and does not invoke the destructor of B. The pointer p points to the object of B.

Moreover, the reason why a destructor is written as a virtual function can invoke destructor B by using a type pointer to delete. is dynamic binding again. However, the premise of dynamic binding is two functions to have a consistent declaration Ah, here is ~a (), is virtual, it can not be said to be dynamically bound to ~b (); Go, at least they're not of the same name.

Open the VC debugging, after the Watch window to see, the value of the pointer p and B, but the "+" after the expansion of the see is not the same, p expanded unexpectedly did not see B's member variables, thus guessing: so, that kind of B unique functions can also not be called (nonsense, who used it so). That is to say, through the pointer p to delete, the call is of course a destructor, no wonder B's destructor is not called.

A destructor of a virtual function found that the B-object in the Virtual function table (Expand the "+" of the pointer p) on the presence of B destructor information, it covers the original a virtual destructor, and can only cover (how to cover.) This one...... Or you don't know which function to call through the base class pointer (so guess right). Then, according to the order of the destructor, the destructor of a is called again.

There is also a question, why is a pointer B to a class A pointer, class B unique members will disappear, and, the virtual function table pointer is also unique to Class B, why it can still stay there. This has to say from the object memory model of C + + Brabra ..., estimates should be related to truncation technology, from a derived hierarchy (see the order of the base class derived classes), to construct a derived class object, to construct the parent class's member variables, and then ... C + + is the first four bytes of the class object structure initialized to the virtual function table pointer (from the point of view of single inheritance), so the truncation is still retained (hehe, yes).

I hope you walk past, there is any correction, the generous enlighten.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.