Is the destructor implicit in the function of the overloaded operator?

I have an Item class that defines its own new operator and the delete operator as follows:

 class Item { public: Item(const std::string &s):msg(s) { std::cout<<"Ctor: "<<msg<<std::endl; } static void* operator new(size_t size, int ID, const std::string &extra) { std::cout<<"My Operator New. ID/extra: "<<ID<<"/"<<extra<<std::endl; return ::operator new(size); } static void operator delete(void* p) { std::cout<<"My Operator Delete"<<std::endl; return; } ~Item() { std::cout<<"Destructor: "<<msg<<std::endl; } void Print() { std::cout<<"Item::msg: "<<msg<<std::endl; } private: std::string msg; }; 

I create an object of this type using the new placement, and then delete it as follows:

 int main() { Item *pI=new(1,"haha")Item("AC Milan"); std::cout<<"before delete"<<std::endl; delete pI; std::cout<<"after delete"<<std::endl; return 0; } 

Output:

 My Operator New. ID/extra: 1/haha Ctor: AC Milan before delete Destructor: AC Milan My Operator Delete after delete 

As you can see, delete pI calls my own delete function, which does nothing but log output. However, from the output, the Item destructor is called in delete pI , which is not called in my own delete function.

So, in this case, the destructor will be called implicitly in the overloaded delete function?

+7
c ++ destructor delete-operator
source share
3 answers

So, in this case, the destructor will be called implicitly in the overloaded delete function?

Yes. To delete an expression , (1) the destructor will be called first, then (2) the apporiate operator delete will be called; name searches and overload resolution will be performed at this point.

If the expression is not a null pointer, the delete expression calls the destructor (if any) for the object to be destroyed or for each array to be destroyed (starting from the last element in the first element of the array).

After that, if the corresponding new expression will not be combined with another new expression (since C ++ 14), the delete expression calls the release function, either operator delete (for the first version of the expression) or operator delete[] (for the second version of the expression).

+4
source share

The destructor is not called by the operator delete() overloaded function.

However, the expression delete (in your case, delete pI ) has the effect of first calling the destructor for the object, and then invokes the corresponding operator delete() overload.

+1
source share

So, in this case, the destructor will be called implicitly in the overloaded delete function?

Destructors and overloaded delete operators interact in a complex way. When you say

 delete pI; 

it actually compiles to call your destructor, and no reference to "delete" is made.

Your destructor has (GCC) two instances in VTable (if virtual) or (MSVC) boolean argument. / Bool instances are used to determine if the compiled destructor should be a delete or a call to the destructor without removal. As you can guess, deleting the destructor is then responsible for calling the telecom operator to delete the link. If you wrote

 pI->~Item(); 

you would compile to run the destructor without deleting (another vtable entry or with boolean flipping).

Since your destructor is not virtual, it is probably all nested anyway. However, the behavior will be indistinguishable from this. That way, if your destructor is virtual, delete will cause the correct deletion for whatever type you have, and not to delete the type of the pointer you call delete on.

0
source share

All Articles