Thread: Constructor with 'new'...

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Any plans to allow it in the next standard?
    According to this article, the answer is yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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,195
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed