L
Lasse Skyum
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?
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?