Thread: Creating array of objects w/o default constructor

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    Creating array of objects w/o default constructor

    So, here's my code...
    Code:
    template <typename RandIter>
    void compare_sorts (RandIter begin, RandIter end, double entropy = 0)
    {
       using std::iterator_traits<RandIter>::value_type;
       value_type * manip_array;
    
       manip_array = new value_type[end - begin];
       std::copy (begin, end, manip_array);
    
       // ...
    }
    Now, my understanding is that whenever you create an array of objects, you automatically call the default constructor for each of the uninitialized values, so the code above calls the default constructor of value_type once for each element.

    The thing is, immediately after creating the array, I assign in the iterated values. In principle, I should be able to rewrite the above code so that it creates manip_array using only the copy constructor.

    Is it possible, and how?
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could use a vector instead of a dynamic array, set the proper capacity (not size), then copy with a back inserter.

    You might also be able to do something complex with placement new, but I'm not sure that would be necessary.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think Daved has the right idea... I was searching Josuttis' standard library book for an answer but could not find one that was suitable.

    In fact, it would be simpler, in the sense that there is a constructor that takes a range, so there is no need to set the capacity then copy with a back inserter.
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> In fact, it would be simpler, in the sense that there is a constructor that takes a range, so there is no need to set the capacity then copy with a back inserter.

    Good call.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Sorry guys, but the vector solutions don't solve the problem at all. It still performs an initialization, followed by a bunch of assignment operations.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    why not using dynamic array so you can perform polymorphism if you are doing some inheritance
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by QuestionC View Post
    Sorry guys, but the vector solutions don't solve the problem at all. It still performs an initialization, followed by a bunch of assignment operations.
    Not necessarily. Only if you use any of the forms which are explicitly documented to do this. If you don't, the contained type doesn't even need a default constructor. Thus:
    Code:
    template <typename RandIter>
    void compare_sorts (RandIter begin, RandIter end, double entropy = 0)
    {
       typedef typename std::iterator_traits<RandIter>::value_type value_type;
       typedef vector_type std::vector<value_type>;
    
       vector_type manip_array(begin, end);
    
       // ...
    }
    This doesn't need the default constructor and is even guaranteed (if RandIter really is random access) to allocate only once. Ergo you have zero overhead compared to the other form.
    Last edited by CornedBee; 04-30-2007 at 10:40 AM.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Sorry guys, but the vector solutions don't solve the problem at all. It still performs an initialization, followed by a bunch of assignment operations.
    Did you implement it correctly?

    >> why not using dynamic array so you can perform polymorphism if you are doing some inheritance
    A dynamic array has nothing to do with polymorphism. Are you suggesting that pointers be stored instead?

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Ok, I didn't implement it correctly... calling the iterator constructor of vector does not call the assignment or default constructors.

    Still, I would like to know how to do this. I'm not a fan of methods I don't understand, but the source for my compiler's implementation of Vector is arcane and undecipherable.
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's pretty simple. std::vector has a constructor that takes two iterators (a "range") as arguments. This constructor just initializes the vector with a copy of this range.

    Or do you mean how it's implemented?
    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

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by QuestionC View Post
    Ok, I didn't implement it correctly... calling the iterator constructor of vector does not call the assignment or default constructors.

    Still, I would like to know how to do this. I'm not a fan of methods I don't understand, but the source for my compiler's implementation of Vector is arcane and undecipherable.
    Code:
    std::vector<T>(Iter start, Iter end)
    This constructor uses copy constructors (or whatever can be used to implement making a copy).

    A vector owns its objects, it will make a copy of the objects it is storing. Do you have a problem with that?

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I mean, how one would implement it.
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why do you need to implement it?

    You can look it up in the "vector" header, although it probably is not going to be nice reading.

    I guess you want to make a copy of the data before passing it to various sort routines? So, you have random access iterators, why not use a vector?

  14. #14
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    the answer to your topic is easy, as long as u keep it simple

    Code:
    Myclass* objects[10];
    
    for(int i = 0; i < 10; i++)
    objects = new Myclass(/*whether you have contructor or nor*/);
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    the answer to your topic is easy, as long as u keep it simple
    That's not going to work for a vector though (only 10 elements?), and I doubt if vectors are typically implemented as a pointer to a pointer.
    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. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  3. Replies: 2
    Last Post: 04-04-2007, 06:34 PM
  4. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  5. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 09:45 PM