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; Originally Posted by brewbuck You're trying to take an object which is inherently not default-constructible, and default-construct it. Failing that, ...

  1. #16
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    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.
    I wasn't trying to default construct it, I was trying to construct it in a very specific way.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Anyway, are you going to take my suggestion of using a container of (smart) pointers instead?
    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. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Angus View Post
    I wasn't trying to default construct it, I was trying to construct it in a very specific way.
    No, you were trying to create an array of objects. That's default construction.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    No, you were trying to create an array of objects. That's default construction.
    No, I was trying to create an array of objects constructed with an explicit set of parameters. That's not default construction

  5. #20
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by laserlight View Post
    Anyway, are you going to take my suggestion of using a container of (smart) pointers instead?
    Well, 'fact is, I've never broken into smart pointers, and right now I'm still trying to cut my teeth on pthreads. I found the next best solution in making an array of pointers, and looping the creation of all my objects, and of course looping their destruction as well. I'll do smart pointers next time. Thanks anyway.

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Angus View Post
    No, I was trying to create an array of objects constructed with an explicit set of parameters. That's not default construction
    An array of objects is default-constructed, period. You can try, but cannot achieve, the impossible...

    I'm with LaserLight that you ought to look at using some kind of smart pointer. Why do you need an array of these things anyway?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #22
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    An array of objects is default-constructed, period. You can try, but cannot achieve, the impossible...
    Right, so I identified containers as the problem, abandoned them, and therein lay an acceptable solution.

    I'm with LaserLight that you ought to look at using some kind of smart pointer. Why do you need an array of these things anyway?
    Why does anyone need an array? As the name suggests, that class represents a thread, and I need multiple threads.

  8. #23
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Angus View Post
    Why does anyone need an array? As the name suggests, that class represents a thread, and I need multiple threads.
    Then the container/array should hold pointers to these objects. Copying a thread obviously makes no sense -- what would it mean?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #24
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    Then the container/array should hold pointers to these objects. Copying a thread obviously makes no sense [snip]
    Ya think?

  10. #25
    Registered User
    Join Date
    May 2007
    Posts
    147
    I've been questioned on this proposal before, and I'm making it here too.

    Smart pointers and threaded programming go together. I've written threaded applications for many years, and at this point I find that writing threaded applications without using smart pointers is more difficult. If I were designing a course study, I'd cover smart pointers before threads; they're that related.

    I've encountered puzzlement on this point before. Threading often brings up a problem with containment; it's common to end up creating objects or containers of objects that are handed over to other threads, and at some point one comes to the question "who deletes this?". Smart pointers (reference counted specifically) solve this problem to the point of triviality (well, almost). It provides a similar feature to the memory management common to Java and C# application work, with respect to threading.

    That has no direct bearing on the point about the array and construction of your original point, just a response to your indication of dealing with smart pointers AFTER you've worked on threads.

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    My current stance on C++ programming is that EVERY class must either be well-behaved when copy-constructed or assigned, OR it must prevent copy-construction and assignment, by declaring those both private and leaving them unimplemented.
    I also believe that an unnecessarily implemented copy-constructor, assignment operator or destructor is very wrong.

    One way or another, your class is broken.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #27
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Angus View Post
    Ya think?
    If it's all so obvious, what's this thread about?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #28
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    If it's all so obvious, what's this thread about?
    Originally it's what I said it was about in my first post. Then it went through a yarn of whether I should change my class to suit containers, or throw out containers to suit my class, then I announced which of those options I would choose. Then it looped right back to weather I should change my class to suit containers or throw out containers to suit my class.

    Now it's just a matter of making sure that the next guy who comes along w/this problem is going to get it right away, and not get lost.

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't understand what all the fuss is about here. Angus wanted to be able to create an array of non-default constructed objects. Use of vector was (appropriately) suggested, at which point is was determined that a vector of objects wouldn't make sense since the objects in question aren't copyable. I fail to see how anything is broken. (Of course, if it hasn't already been done, the JpegThread class should disable copying so that a compiler error is given when it is used with vector or otherwise accidentally copied.)

    So, Angus has decided on a container of raw pointers for a perfectly legitimate reason.

    Angus, if and when you have time to invest in it, I'd use Boost's ptr_vector over a vector of smart pointers for this solution. You'll have to use boost or TR1 for this anyway (because there is no standard smart pointer suitable for use with a vector), so you might as well use ptr_vector which does a nice job of implementing exactly what you're implementing now, only with a more widely used and tested interface and implementation.

Page 2 of 2 FirstFirst 12
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