By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 927 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

replacing operator new

P: n/a
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 ::operator 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
Apr 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mark P wrote:
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 ::operator 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. ::operator 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

Apr 21 '06 #2

P: n/a
mlimber wrote:
Mark P wrote:
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 ::operator 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. ::operator 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
Apr 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.