Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly Overloading new/delete new[]/delete[]

This is a follow up to my previous question,

Initializing a class using malloc

Accepted answer on the question works and gives me new/delete on the avr-gcc, here is the problem but my overloaded new delete wracks havoc on regular gcc, what is the proper way to overload new delete all my classes derive from a common base class so ideally i would like to just override new delete for my object so it does not mess with stl stdlib etc.

like image 553
Hamza Yerlikaya Avatar asked May 02 '11 15:05

Hamza Yerlikaya


1 Answers

'new' and 'delete' can overloaded inside the common Object base class. So, that will be applicable only to that hierarchy.

class Object {
public:
  void* operator new (size_t size);
  void operator delete (void *p);
};

class Derived : public Object {
// uses the above versions of new/delete
};

[Note: It's an added advantage for you as all your class are getting derived from a common Object class (as mentioned in your question and the link)]

like image 174
iammilind Avatar answered Oct 13 '22 02:10

iammilind