replacing operator new

M

Mark P

Consider a class in which I redefine operator new(std::size_t):

struct A
{
void* operator new(std::size_t size) {/* my implementation */}
};

This has the consequence of hiding the placement version of operator new:

void* operator new(std::size_t size, void* location);

I can deal with this by defining placement new within A to call the
global placement new:

void* operator new(std::size_t size, void* location)
{
return ::eek:perator new(size,loc);
}

Is this the recommended way to handle this situation? Is it possible to
achieve the same effect by some sort of using declaration?

Thanks,
Mark
 
M

mlimber

Mark said:
Consider a class in which I redefine operator new(std::size_t):

struct A
{
void* operator new(std::size_t size) {/* my implementation */}
};

This has the consequence of hiding the placement version of operator new:

void* operator new(std::size_t size, void* location);

I can deal with this by defining placement new within A to call the
global placement new:

void* operator new(std::size_t size, void* location)
{
return ::eek:perator new(size,loc);
}

Is this the recommended way to handle this situation? Is it possible to
achieve the same effect by some sort of using declaration?

Thanks,
Mark

_C++ Coding Standards_ by Sutter and Alexandrescu recommends in Item 46
that if you provide any class-specific new, provide all of the standard
forms (plain, placement, and nothrow). Otherwise, any form you don't
provide will be hidden from users of your class (e.g., the STL, which
uses placement new extensively).

If you have a base class that defines new operators, you can hoist them
into your derived class with "using", but you can't pull in names from
the global namespace (e.g. ::eek:perator new) that way, so you'll have to
write forwarding functions to the global operators (as you did above).

Also, don't forget to supply a matching delete operator for each
non-placement new operator that you provide. Failing to do so is bad
(see _C++ Coding Standards_, Item 45).

Cheers! --M
 
M

Mark P

mlimber said:
_C++ Coding Standards_ by Sutter and Alexandrescu recommends in Item 46
that if you provide any class-specific new, provide all of the standard
forms (plain, placement, and nothrow). Otherwise, any form you don't
provide will be hidden from users of your class (e.g., the STL, which
uses placement new extensively).

If you have a base class that defines new operators, you can hoist them
into your derived class with "using", but you can't pull in names from
the global namespace (e.g. ::eek:perator new) that way, so you'll have to
write forwarding functions to the global operators (as you did above).

Also, don't forget to supply a matching delete operator for each
non-placement new operator that you provide. Failing to do so is bad
(see _C++ Coding Standards_, Item 45).

Cheers! --M

Thanks for the clarifications.

Mark
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top