Click to See Complete Forum and Search --> : C++ new operator weird syntax


leonardo1
February 24th, 2008, 08:22 AM
Hi,

I was reading the E Appendix of "The C++ Programming Language , Special Edition" when I came across on page 948 with the following syntax:
"new(b.space) T(x);" where b.space is a pointer and x an object of type T. Does this syntax works in modern C++ compilers?

Thank you

laserlight
February 24th, 2008, 08:31 AM
Why not test and find out for yourself? Or if you want others to help you test, then post the smallest and simplest program that you expect to be compilable. Not everyone has a copy of TC++PL.

Hermit
February 24th, 2008, 08:45 AM
This is called placement new, and basically it's for constructing objects at a particular (pre-allocated) memory address without allocating anything. In the TC++PL example, a copy of x will be constructed at the address pointed to by b.space.

Lindley
February 24th, 2008, 09:10 AM
In any reasonably optimizing compiler, this is identical to

*b.space = T(x);


Assuming a copy constructor or operator= exists for the class. If x really is of type T, then I assume it does; in which case it's effectively the same as


*b.space = x;