Thread: Creating an array of object w/no default constructor

  1. #1
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115

    Creating an array of object w/no default constructor

    I have this problem where I want to use the new operator to create an array, and pass the constructors the same data. I've tried this
    Code:
    pThreads = new JpegThread[nThreadCount](cdnWaiting, nOriginalWidth, nOriginalHeight, nWidth, nHeight);
    and this
    Code:
    pThreads = new JpegThread(cdnWaiting, nOriginalWidth, nOriginalHeight, nWidth, nHeight)[nThreadCount];
    but nothing seems to appease the compiler. What am I doing wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angus
    What am I doing wrong?
    Trying to create an array of objects of a type without a default constructor

    Anyway, this is a simple solution:
    Code:
    std::vector<JpegThread> pThreads(nThreadCount,
        JpegThread(cdnWaiting, nOriginalWidth, nOriginalHeight, nWidth, nHeight));
    If you want a more complex solution then look into the use of placement new.
    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
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    So there's no solution that doesn't involve STL's vector or placement new? Ok, I'll use that. Thanks

    If placement new is what I think it is, I took one look at it and swore I'd never use it.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why the aversion against STL's vector? It is such a useful tool that one (practically) never needs to use new[] / delete[].

    The placement new thing is probably rather ugly: this is what std::vector actually does for you.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    When the implementation of something is extremely ugly, usually the language is trying to tell you something... I.e. your whole approach isn't right.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by anon View Post
    Why the aversion against STL's vector? It is such a useful tool that one (practically) never needs to use new[] / delete[].
    Because, all I wanted to do is 1 new, 1 delete, and a whole lot of [] references. Beside, my expressions were already getting pretty long without
    Code:
    std::vector<JpegThread>::this_::that:and_his_brother

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angus
    Because, all I wanted to do is 1 new, 1 delete, and a whole lot of [] references. Beside, my expressions were already getting pretty long without
    You can still use operator[] with std::vector<JpegThread>, and the use of typedefs can help if you find yourself needing to create iterators or use std::vector<JpegThread>::size_type often.
    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

  8. #8
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    And now I've got another reason to be averse to STL's vector: JpegThread can't be copied, and std::vector does that (behind the scenes, which is why it took me 30 minutes to figure out what was wrong)
    So I'm back to my original problem: how can I do this using a simple array (created with new []) so that I know exactly what's going on, w/out any of that STL cleverness?

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Angus View Post
    And now I've got another reason to be averse to STL's vector: JpegThread can't be copied, and std::vector does that (behind the scenes, which is why it took me 30 minutes to figure out what was wrong)
    So I'm back to my original problem: how can I do this using a simple array (created with new []) so that I know exactly what's going on, w/out any of that STL cleverness?
    I don't understand your complaints about std::vector. JpegThread can't be copied because it's a RAII object. These sorts of things should not be placed into containers. That is NOT the fault of std::vector, it is a fault in your design.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Angus
    And now I've got another reason to be averse to STL's vector: JpegThread can't be copied, and std::vector does that (behind the scenes, which is why it took me 30 minutes to figure out what was wrong)
    Yes, all the standard containers require that the objects they store be copyable.

    Quote Originally Posted by Angus
    So I'm back to my original problem: how can I do this using a simple array (created with new []) so that I know exactly what's going on, w/out any of that STL cleverness?
    Use a container of (smart) pointers to JpegThread instead.

    EDIT:
    Quote Originally Posted by brewbuck
    JpegThread can't be copied because it's a RAII object. These sorts of things should not be placed into containers.
    "RAII object" is ambiguous. You are saying that JpegThread has auto_ptr copying semantics?
    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

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    how can I do this using a simple array (created with new []) so that I know exactly what's going on
    It is quite likely that you won't know better. E.g: what happens if an exception is thrown from some place?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    "RAII object" is ambiguous. You are saying that JpegThread has auto_ptr copying semantics?
    I mean RAII in the most general sense -- an object which acquires resources in its constructor and releases them in its destructor. Copying of such objects usually makes no sense because the underlying resource can't be duplicated.

    A RAII object might support some kind of resource sharing and lightweight copying, but I don't think that's a requirement to call something "RAII."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    I don't understand your complaints about std::vector. JpegThread can't be copied because it's a RAII object. These sorts of things should not be placed into containers. That is NOT the fault of std::vector, it is a fault in your design.
    Says who?

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Angus View Post
    Says who?
    You're trying to take an object which is inherently not default-constructible, and default-construct it. Failing that, you're trying to take an object which is non-copyable, and copy it. How many other impossible things are you willing to try?

    Blaming the language isn't the answer.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by brewbuck
    I mean RAII in the most general sense -- an object which acquires resources in its constructor and releases them in its destructor. Copying of such objects usually makes no sense because the underlying resource can't be duplicated.
    Ah, but then the focus should not be on RAII; it should be on the fact that the object is not copyable.

    Quote Originally Posted by brewbuck
    A RAII object might support some kind of resource sharing and lightweight copying, but I don't think that's a requirement to call something "RAII."
    That normal copying semantics does not make sense is not a requirement to call something "RAII" either.
    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

Similar Threads

  1. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 12:40 AM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM