473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Apr 20 '06 #1
2 2306
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rutger Claes | last post by:
How do I stop the SAX parser from replacing entities such as ã and ©. When I use > and a default_handler, the default_handler is called and I can just print the data. Entities as ©...
2
by: sqwirx | last post by:
I am using a String that has a member function c_str(). Because I think c_str() reduces the readability of the code and I don't want implicit conversions, I'm concidering to add a "char const *...
6
by: Jay Nabonne | last post by:
Hi, This might sound odd, but we want to replace the allocation scheme used by new and delete without changing operator new and operator delete. (The global operators are shared and we can't...
8
by: Joe Van Dyk | last post by:
I have a vector of structs. Say I get a new struct that's supposed to replace one of the structs in the vector. I believe I could use replace_if() to do this, but I'm not sure what the syntax...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
5
by: RS24 | last post by:
Hi! Can someone tell me the correct answer for these ... Replace the following code using new operator- (Assume int is 2 bytes) #define MAXROW 3 #define MAXCOL 4 1.) void main()
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
1
by: patelgaurav85 | last post by:
Hi, I want to convert xml in one format into another xml format shown below Input xml : <Name> <Name1> <Name11>Name11</Name11> <Name12>Name12</Name12>
8
by: Choi | last post by:
Hi ! I wonder how I can replace a quote by a "\n" in a string. For example my string is rrrrrrr'ttttttt and I want to obtain : rrrrrrr ttttttt I tried the function find, but I don't know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.