STL vector and element constructors

Discussion in 'C++' started by Jan, Dec 9, 2005.

  1. Jan

    Jan Guest

    Hi there,
    I've created a reference to a new object (with private copy
    constructor)
    inside a STL vector

    object& obj = aVector[aVector.size()+1]

    Is there any possibility to define, which constructor for obj is used?
    (In this case, obj() is called, but i want something like obj(xyz) )

    Thanks,
    Jan
     
    Jan, Dec 9, 2005
    #1
    1. Advertising

  2. * Jan:
    > Hi there,
    > I've created a reference to a new object (with private copy
    > constructor)
    > inside a STL vector


    Can't do that; standard collection elements must be copyable.


    > object& obj = aVector[aVector.size()+1]


    Can't do that, either; figure out why (besides, there is a missing
    semicolon).


    > Is there any possibility to define, which constructor for obj is used?


    No constructor is used above.


    > (In this case, obj() is called, but i want something like obj(xyz) )


    That's meaningless; please reformulate.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 9, 2005
    #2
    1. Advertising

  3. Jan

    Dervish Guest

    Your code have a bug - it references non existent element. Operator []
    does not create new element. Program can crush, constructor (any)
    should no be called at all. To create new object using [] your should
    use map.

    To fix this using vector one can do smth like this:
    aVector.resize(aVector.size()+1);
    object& obj = aVector.back();

    >Is there any possibility to define, which constructor for obj is used?

    No. The only was is to create custom class which calls proper ctor.
    E.g.:
    class ProperConstructObject : public object
    {
    public:
    ProperConstructObject()
    : object(xyz)
    {

    }
    //...
    };
    Where xyz should be either global symbol or class member (may be
    static).
     
    Dervish, Dec 9, 2005
    #3
  4. Jan

    benben Guest

    > object& obj = aVector[aVector.size()+1]

    The above does not create a new element for you. What the above does is
    to give a new name to the second element following the last element. So
    if aVector has 10 elements already, obj would be a new name for the 12th
    element in aVector.

    Ben
     
    benben, Dec 9, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,396
    Martijn Lievaart
    Feb 6, 2004
  2. Replies:
    8
    Views:
    1,998
    Csaba
    Feb 18, 2006
  3. Jeremy Smith
    Replies:
    2
    Views:
    621
    Jeremy Smith
    Aug 3, 2006
  4. Jess
    Replies:
    5
    Views:
    640
    Ron Natalie
    Jun 7, 2007
  5. Peng Yu
    Replies:
    5
    Views:
    410
    Juha Nieminen
    Sep 19, 2008
Loading...

Share This Page