5

Is it possible to overload class specific new/delete that is called when arrays of objects are created.

class Foo;

Foo* f = new Foo[10]; // calls overloaded new
delete[] f; // calls overloaded delete

Thank you.

pic11
  • 14,267
  • 21
  • 83
  • 119
  • See http://stackoverflow.com/questions/7149461/why-should-one-replace-default-new-and-delete-operators and http://stackoverflow.com/questions/7149461/why-should-one-replace-default-new-and-delete-operators. – Keith Layne Dec 17 '11 at 06:38

1 Answers1

8

Yes, it is possible. There is a tutorial about overloading new and delete here, and there is a nice example of overloading new and delete for array, here.

class Myclass
{
  public:
        void* operator new(size_t); 
        void operator delete(void*);

        void* operator new[](size_t); 
        void operator delete[](void*);
};
Igor
  • 26,650
  • 27
  • 89
  • 114