why vector assignment operator not invoked on const vector& ?

Discussion in 'C++' started by zhou, Sep 2, 2003.

  1. zhou

    zhou Guest

    Hi there,

    I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
    vector & type), which makes a copy of the vector returned from myFunc():

    vector<myObj*> anotherVec = myFunc();

    const vector<myObj *>& myFunc()
    {
    vector <myObj *> localVec;

    // add objects into localVec

    return localVec;
    }

    However, my understanding is that assignment operator takes const Object& type. However, the assignment operator on
    vector is not invoked here and therefore vector is not copied. Am I missing anything?

    Thanks.

    Yan
     
    zhou, Sep 2, 2003
    #1
    1. Advertising

  2. zhou

    Ron Natalie Guest

    "zhou" <> wrote in message news:bj2cq2$e5h5j$-berlin.de...
    > Hi there,
    >
    > I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
    > vector & type), which makes a copy of the vector returned from myFunc():
    >
    > vector<myObj*> anotherVec = myFunc();


    It has nothing to do with constness. The above is NOT an assignment, it is initialization.
    The copy-constructor is what is invoked.
     
    Ron Natalie, Sep 2, 2003
    #2
    1. Advertising

  3. zhou wrote:
    >
    > Hi there,
    >
    > I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
    > vector & type), which makes a copy of the vector returned from myFunc():
    >
    > vector<myObj*> anotherVec = myFunc();
    >
    > const vector<myObj *>& myFunc()
    > {
    > vector <myObj *> localVec;
    >
    > // add objects into localVec
    >
    > return localVec;
    > }
    >
    > However, my understanding is that assignment operator takes const Object& type. However, the assignment operator on
    > vector is not invoked here and therefore vector is not copied. Am I missing anything?
    >


    Yep.
    The above is *not* an assignment. It is an initialization, hence the
    copy constructor is called (or not, if it is optimized away by the compiler).


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 2, 2003
    #3
  4. zhou wrote:
    > Hi there,
    >
    > I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
    > vector & type), which makes a copy of the vector returned from myFunc():
    >
    > vector<myObj*> anotherVec = myFunc();


    This is initialization, not assignment.

    > const vector<myObj *>& myFunc()
    > {
    > vector <myObj *> localVec;
    >
    > // add objects into localVec
    >
    > return localVec;
    > }
    >
    > However, my understanding is that assignment operator takes const Object& type. However, the assignment operator on
    > vector is not invoked here and therefore vector is not copied. Am I missing anything?


    The copy constructor is supposed to be invoked. However, returning a
    reference to a local object is illegal in C++ It causes undefined
    behavior. That's what's happening in your code.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP
     
    Andrey Tarasevich, Sep 2, 2003
    #4
  5. "zhou" <> wrote in message
    news:bj2jb1$eeu7p$-berlin.de...
    > I see. However, why the copy constructor does not copy the vector?
    >
    > Yan


    It does copy the vector, what else would it do?

    Why do you think it does not copy the vector?

    I think you should post some real.

    john
     
    John Harrison, Sep 2, 2003
    #5
  6. "John Harrison" <> wrote in message
    news:bj2nq6$eall7$-berlin.de...
    >
    > "zhou" <> wrote in message
    > news:bj2jb1$eeu7p$-berlin.de...
    > > I see. However, why the copy constructor does not copy the vector?
    > >
    > > Yan

    >
    > It does copy the vector, what else would it do?
    >


    Might have been too hasty with my last post. I think I'll bow out now.

    john
     
    John Harrison, Sep 2, 2003
    #6
  7. zhou

    jeffc Guest

    "John Harrison" <> wrote in message
    news:bj2nq6$eall7$-berlin.de...
    >
    > I think you should post some real.


    code.
     
    jeffc, Sep 2, 2003
    #7
  8. zhou wrote:

    > I see.


    I don't, since you didn't bother to quote any context.

    > However, why the copy constructor does not copy the vector?


    A vector's copy constructor does copy. If you have code demonstrating a
    case where it doesn't, post it. Otherwise I have no idea what you are
    talking about.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 2, 2003
    #8
  9. zhou

    zhou Guest

    Thanks. You are right. The copy consturctor is invoked after the local object is destroyed, thus returning a const &
    does no good: it does invoke copy constructor on an already emptied vector.
     
    zhou, Sep 2, 2003
    #9
  10. zhou wrote:

    > Thanks. You are right. The copy consturctor is invoked after the local object is destroyed, thus returning a const &
    > does no good: it does invoke copy constructor on an already emptied vector.


    It is not that it is "emptied". The storage duration of the local vector
    is already over. The vector doesn't exist anymore. That's why your code
    produces undefined behavior.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP
     
    Andrey Tarasevich, Sep 2, 2003
    #10
    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. Tim Clacy
    Replies:
    15
    Views:
    2,689
    Kanenas
    May 30, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,974
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    11
    Views:
    1,108
  4. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  5. Replies:
    2
    Views:
    541
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page