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(){pri ntf("+");} // construct
CMyClassA(const CMyClassA &a){printf("c") ;} // copy construct
virtual ~CMyClassA(){pr intf("-");} // destruct
};
void test(){
CMyClassA q;
vector<CMyClass A> 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
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(){pri ntf("+");} // construct
CMyClassA(const CMyClassA &a){printf("c") ;} // copy construct
virtual ~CMyClassA(){pr intf("-");} // destruct
};
void test(){
CMyClassA q;
vector<CMyClass A> 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
Comment