more about vectors

Discussion in 'C++' started by Lasse Skyum, Nov 9, 2003.

  1. Lasse Skyum

    Lasse Skyum Guest

    Suppose i have a std::vector containing 2 elements. If I delete the first
    element, the second element will be copied to the first position. A little
    test showed that the deleted element was indeed destructed, but there was
    never executed a copy-constructor for moving the remaining element??


    class CMyClassA
    {
    public:
    CMyClassA(){printf("+");} // construct

    CMyClassA(const CMyClassA &a){printf("c");} // copy construct

    virtual ~CMyClassA(){printf("-");} // destruct
    };

    void test(){
    CMyClassA q;
    vector<CMyClassA> a;
    a.push_back(q); // Insert two elements
    a.push_back(q);
    printf("|");
    a.erase(a.begin(),a.begin()+1); // Remove the first element
    printf("|");
    }

    This test prints "+ccc-|-|--" , but I would have expected there to be a 'c'
    somewhere between the two | |.

    Can anyone explain this?

    --
    Lasse
     
    Lasse Skyum, Nov 9, 2003
    #1
    1. Advertising

  2. Lasse Skyum

    Max M Guest

    "Lasse Skyum" <no spam> wrote:
    >
    > Suppose i have a std::vector containing 2 elements. If I delete the first
    > element, the second element will be copied to the first position. A little
    > test showed that the deleted element was indeed destructed, but there was
    > never executed a copy-constructor for moving the remaining element??


    How can you know the destructed object is indeed the erased element?
    I guess the values are shifted through assignment, and then the last
    element is destructed. (Redefine assignment to keep track of when it gets
    called, and print the object's address to know who gets really destructed.)

    Max
     
    Max M, Nov 9, 2003
    #2
    1. Advertising

  3. Lasse Skyum

    Lasse Skyum Guest


    > How can you know the destructed object is indeed the erased element?


    Hmmm... guess that was kind of an assumption...

    > I guess the values are shifted through assignment, and then the last
    > element is destructed. (Redefine assignment to keep track of when it gets
    > called, and print the object's address to know who gets really

    destructed.)

    Yes, you are right! The element is simply assigned, not destructed and
    copy-constructed again as I thought it would be.

    --
    Lasse
     
    Lasse Skyum, Nov 9, 2003
    #3
    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. shess
    Replies:
    0
    Views:
    300
    shess
    Nov 24, 2003
  2. Michael
    Replies:
    4
    Views:
    467
    Matt Hammond
    Jun 26, 2006
  3. Replies:
    3
    Views:
    725
    Shadowman
    Mar 26, 2008
  4. Guest
    Replies:
    0
    Views:
    481
    Guest
    Sep 14, 2005
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    236
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page