Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > more about vectors

Reply
Thread Tools

more about vectors

 
 
Lasse Skyum
Guest
Posts: n/a
 
      11-09-2003
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


 
Reply With Quote
 
 
 
 
Max M
Guest
Posts: n/a
 
      11-09-2003
"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
 
Reply With Quote
 
 
 
 
Lasse Skyum
Guest
Posts: n/a
 
      11-09-2003



> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ primer statement about vectors containing vectors pauldepstein@att.net C++ 3 03-26-2008 06:22 PM
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Ruby, SWIG and C++: how to properly wrap vector of vectors of doubles (2D vectors)? Ruby 0 09-14-2005 05:47 PM
Comparison of Bit Vectors in a Conditional Signal Assignment Statement Anand P Paralkar VHDL 2 08-04-2003 08:40 PM



Advertisments