This problem seems strange, but I checked several compilers. In my code I have Move Constructor
and copy constructor
how
class A {
int val;
public:
A(int var) : val(var) {
}
A( A && a1) {
cout<<"M Value -> "<<a1.val<<endl;
cout<<"Move Cons..."<<endl;
}
A(const A & a1) {
cout<<"Copy Cons.."<<endl;
cout<<"Value -> "<<a1.val<<endl;
}
};
If I write my function main
as
int main()
{
vector<A> v1;
A a2(200);
v1.push_back(move(a2));
}
Output signal
M Value -> 200
Move Cons...
Expected, but if I change my function main
as
int main()
{
vector<A> v1;
A a2(200);
v1.push_back(A(100));
v1.push_back(move(a2));
}
I get the following output
M Value -> 100
Move Cons...
M Value -> 200
Move Cons...
Copy Cons.. // unexpected
Value -> 0 // unexpected
Can someone help me figure out where and how this one copy constructor
is being called ... this too with meaning0
thank
source
share