I tried to implement this:
namespace Test
{
void* operator new(size_t s)
{
return malloc(s);
}
}
But g++ (4.3.1) says:
void* Test::operator new(size_t)’ may not be declared within a namespace
Am I doing something wrong?
If yes, is there anyway to overload the operator new to be used in my classes? I do not want to create a base class and make all my classes inherit from such base class.
You can only (re-)define
operator newas a member of the global namespace or as (an implicitly static) member of a class.If you don’t have a common base class then you need to define
operator newfor each class that you want a specialized implementation for. You could, of course, delegate to a common global function.