CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2008
    Posts
    1

    C++ new operator weird syntax

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ new operator weird syntax

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: C++ new operator weird syntax

    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.
    - Alon

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ new operator weird syntax

    In any reasonably optimizing compiler, this is identical to
    Code:
    *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

    Code:
    *b.space = x;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured