Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: If I overload new operator, do I have to overload the delete operator too?

I was reading a question on SO and in one of the answers, it has been mentioned as:

If no unambiguous matching deallocation function can be found, propagating the exception does not cause the object’s memory to be freed.

So, if I just overload my new operator and not the delete operator, would any default delete operator be created and called; or, do I have to also write the delete operator explicitly.

like image 330
pasha Avatar asked Sep 04 '18 02:09

pasha


1 Answers

What this means is that if you overload operator new with extra arguments, and not the corresponding delete with extra arguments, if an exception occurs in a constructor, no delete operator will be called. On the other hand, if you're overloading the basic new (with no extra arguments), and an exception occurs, delete with no extra argument will be called, and that will be the default operator delete if you have not overloaded it.

like image 56
Chris Dodd Avatar answered Nov 04 '22 22:11

Chris Dodd