 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mark P Guest
|
Posted: Thu Apr 20, 2006 10:06 pm Post subject: replacing operator new |
|
|
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 |
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Fri Apr 21, 2006 1:06 pm Post subject: Re: replacing operator new |
|
|
Mark P wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Mark P Guest
|
Posted: Fri Apr 21, 2006 6:07 pm Post subject: Re: replacing operator new |
|
|
mlimber wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|