Constructor with 'new'...

This is a discussion on Constructor with 'new'... within the C++ Programming forums, part of the General Programming Boards category; Hi all, I would have a question: When I want to construct an object (with some parameter, e.g. string) in ...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question Constructor with 'new'...

    Hi all,
    I would have a question:
    When I want to construct an object (with some parameter, e.g. string) in a normal way, I use:
    Code:
    SimpleClass simpleObject("ObjectID");
    When I want to construct multiple objects without parameters in "dynamic" way (for example, 4 objects), I use:
    Code:
    SimpleClass* pSimpleObject = new SimpleClass[4];

    But what is the code, when I would like to construct multiple objects in dynamic way "WITH" some parameters?

    Code:
    SimpleClass* pSimpleObject = ??? // new SimpleClass("ObjectID")[4], or what?

    Thanks.

    Petike

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Not possible, at least at the moment. You can simulate it with placement new, but that can be ugly and error prone, so it would be much simpler to just pick a container like std::vector and do:
    Code:
    std::vector<SimpleClass> simpleObjects(4, SimpleClass("ObjectID"));
    The placement new trick would be done under the hood, plus you get the benefit of not having to do manual memory management.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've always wondered why they didn't put in support for that too. Seems like a common enough thing to want to do.
    Any plans to allow it in the next standard?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Any plans to allow it in the next standard?
    According to this article, the answer is yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The real question will be when will the full support for C++0x be enforced by your compiler.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    The real question will be when will the full support for C++0x be enforced by your compiler.
    I am not sure if any compiler has full support for C++03, so the answer might well be "never".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    But change it to "reasonable", and the answer is somewhere between 2010 and 2013.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    plus you get the benefit of not having to do manual memory management.
    ah yes, 'benefit', snicker....

    There is a major product out on release right now that is essentially still born because they took advantage of this 'benefit'. Now they have memory leaks that they cant track down. While this can have advantages, its not a magic bullet solution to memory management.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    There is a major product out on release right now that is essentially still born because they took advantage of this 'benefit'. Now they have memory leaks that they cant track down. While this can have advantages, its not a magic bullet solution to memory management.
    Stroustrup notes the possible use of garbage collection for "litter collection" in such a case (but again GC is not a magic bullet solution for memory management either). I reason that the project in question did not "systematically and correctly" use "RAII plus smart pointers".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21