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
  • 13,366
  • 17
  • 76
  • 110
  • 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
  • 25,778
  • 27
  • 87
  • 113