esoteric use of the "new" keyword
-
I came across this use of the "new" keyword in a library recently. I had to look it up to see what was going on. Apparently its valid, I've just never seen it before in any code examples, books, articles, etc. If you write a custom new operator, you can write it to take an extra argument. When callers use the new keyword they supply an additional argument which gets passed to the new operator. So you can call MyClass* instance = new 128 MyClass; This creates an instance of MyClass as expected but when invoking the custom new operator passes along the argument 128. Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.
-
I came across this use of the "new" keyword in a library recently. I had to look it up to see what was going on. Apparently its valid, I've just never seen it before in any code examples, books, articles, etc. If you write a custom new operator, you can write it to take an extra argument. When callers use the new keyword they supply an additional argument which gets passed to the new operator. So you can call MyClass* instance = new 128 MyClass; This creates an instance of MyClass as expected but when invoking the custom new operator passes along the argument 128. Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.
Have not seen it done like that before, but I could see it being used for things like allocating additional memory at the end of the object. Remember how variable-length structures like
DDEDATA
,PDH_COUNTER_INFO
and some device driver ones worked? Also for custom heap management - I want this object on this heap and that object created on on that heap, or created in a pre-reserved (shared?) block of memory. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
I came across this use of the "new" keyword in a library recently. I had to look it up to see what was going on. Apparently its valid, I've just never seen it before in any code examples, books, articles, etc. If you write a custom new operator, you can write it to take an extra argument. When callers use the new keyword they supply an additional argument which gets passed to the new operator. So you can call MyClass* instance = new 128 MyClass; This creates an instance of MyClass as expected but when invoking the custom new operator passes along the argument 128. Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.
Dave Calkins wrote:
Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.
Placement new, perhaps?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Dave Calkins wrote:
Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.
Placement new, perhaps?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Placement
new
required the use of parenthesis:new (value) Type
Although now that I think about it, you may be correct. I think that placement
new
is the only way to pass parameters tonew
...? Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Placement
new
required the use of parenthesis:new (value) Type
Although now that I think about it, you may be correct. I think that placement
new
is the only way to pass parameters tonew
...? Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFilesJames R. Twine wrote:
I think that placement new is the only way to pass parameters to new...?
AFAIK, you can override the new operator. I think that's what is done for the MFC: the file and line are passed to the new operator in order to track memory leaks easier.
Cédric Moonen Software developer
Charting control [v1.3] -
James R. Twine wrote:
I think that placement new is the only way to pass parameters to new...?
AFAIK, you can override the new operator. I think that's what is done for the MFC: the file and line are passed to the new operator in order to track memory leaks easier.
Cédric Moonen Software developer
Charting control [v1.3]Yep - yer right...
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
James R. Twine wrote:
I think that placement new is the only way to pass parameters to new...?
AFAIK, you can override the new operator. I think that's what is done for the MFC: the file and line are passed to the new operator in order to track memory leaks easier.
Cédric Moonen Software developer
Charting control [v1.3]Cedric Moonen wrote:
AFAIK, you can override the new operator. I think that's what is done for the MFC
GDI+ does it too :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Dave Calkins wrote:
Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.
Placement new, perhaps?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Correct. As I said, I had to look it up to see what was going on and discovered placement new in the docs. My point was just that I'd never seen this used in code before and was curious if anyone else had. It just seems a bit esoteric :)