operator new in custom namespace

D

dirk

Why doesn't the standard allow to overload operator new/delete in my
namespace, e.g.

namespace Foo
{
void* operator new( std::size_t sz );
void operator delete( void* address );
}

On the other hand I can overload new/delete per class:

class Foo1
{

static void* operator new( std::size_t sz );
static void operator delete( void* address );

};

And finally what happens if I have classes A and B that overwrite new
and delete and a class C that now inherits from both. What is the
expected behaviour there?



Cheers,
-Dirk
 
V

Victor Bazarov

Why doesn't the standard allow to overload operator new/delete in my
namespace, e.g.

namespace Foo
{
void* operator new( std::size_t sz );
void operator delete( void* address );
}

On the other hand I can overload new/delete per class:

class Foo1
{

static void* operator new( std::size_t sz );
static void operator delete( void* address );

'static' is implicit here, IIUIC. IOW, you don't need to use the
keyword in the function declaration.
};

And finally what happens if I have classes A and B that overwrite new
and delete and a class C that now inherits from both. What is the
expected behaviour there?

Not sure what having operator new in its own namespace would do. The
language says that if a class has operator new overloaded, then the
operator is used to allocate memory when constructing a dynamic object
of that class (chosen over the global operator new). What are you
trying to accomplish with the overloaded operator new in a namespace?

V
 
J

James Kanze

Why doesn't the standard allow to overload operator new/delete in my
namespace, e.g.
namespace Foo
{
void* operator new( std::size_t sz );
void operator delete( void* address );
}

Historical reasons, I would imagine.
On the other hand I can overload new/delete per class:
class Foo1
{
static void* operator new( std::size_t sz );
static void operator delete( void* address );
};
And finally what happens if I have classes A and B that overwrite new
and delete and a class C that now inherits from both. What is the
expected behaviour there?

Ambiguous. The compiler should complain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top