The "Fix world View" of C + + 's Delete and operator delete overloads. __c++

Source: Internet
Author: User
the Delete and operator delete overloads of C + +. Chao Hua

Just learn C + + when I learned in C + + Delete/new is actually a method, we execute delete p is actually called the method operator delete (p);
Of course, we also learned that delete/new have global and local (class), we can overload them, let delete and new to do the same as we want to the creation and destruction of objects.
For so many years, I have been convinced that the delete p in our code is equivalent to operator delete (void*). It is naïve to think that a destructor call is after the operator delete (void*) method. shock.

In order to maintain the code for a long time, I need to inherit the base class to create a new drive class, while in the existing code, where the base class is used before the delete pbase; The new drive class is not needed/cannot be released, and the problem comes: like the delete pbase, where there are hundreds of code, distributed across different module codes maintained by different departments, there is really no way to gracefully modify the construction/release usage of the entire base class.
So the simplest and most outrageous idea is to delete pbase, which is invalidated when the instance is drive. The idea is to overload the operator delete, add an internal tag, and only the delete can really work when marked as free.

But, the reality is always more than imagine bone feeling.

Confidently compile the code and run it. The program does not run as you want. Only then found:

Delete pbase; Not base::operator Delete (pbase)
Delete pbase; Not base::operator Delete (pbase)
Delete pbase; Not base::operator Delete (pbase)

Delete PBase Delete is a C + + keyword.

It is in the delete pbase expression, which means that you want to destroy the object that pbase points to.
Then delete calls the drive destructor first, then the base destructor, then the operator delete () call. This order is very important. operator delete () is invoked after the destructor. So there's really no way to prevent the execution of the delete pbase by overloading operator delete ().

Of course, if we add a flag to the destructor and operator delete (), they're not going to do anything. Theoretically this can actually prevent the object from being destroyed, or, more accurately, after the delete pbase, the object that PBase points to is still on the memory space (win10 vs2012 x64 test), but the C + + standard is undefined for such behavior (we have found, There is no clear evidence that I can convince myself to submit such a code. make a summary. Delete is a C + + keyword and cannot be equated with the delete operator. The destructor and delete operator methods are called behind the Delete keyword, and destructors are executed first. When you find that the method in your subclass is inconsistent with the usage pattern/behavior pattern of the method with the same name as the parent class, this indicates that your code design is problematic. The polymorphic encapsulation is actually corrupted.

Related Article

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.