Creating an array of object w/no default constructor

This is a discussion on Creating an array of object w/no default constructor within the C++ Programming forums, part of the General Programming Boards category; I have this problem where I want to use the new operator to create an array, and pass the constructors ...

  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
    21,394
    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.
    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
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    21,394
    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.
    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

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    21,394
    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?
    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

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    21,394
    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.
    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

Page 1 of 2 12 LastLast
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, 08:51 AM
  3. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-12-2002, 11:40 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12: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

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