I study the semantics of movement in C ++ 11. I wrote a small program to test the behavior of movement semantics. But this does not behave as I expected, can someone explain to me why?
#include<iostream>
using namespace std;
class Vector
{
public:
Vector()
{
cout << "empty Ctor"<<endl;
array = new int[10];
size = 10;
}
Vector(int n)
{
array = new int[n];
size = n;
for (int i=0; i<size; ++i)
array[i] = i;
cout << "Ctor"<<endl;
}
Vector(const Vector& v):size(v.size)
{
array = new int[size];
for (int i=0; i<size; ++i)
array[i] = v.array[i];
cout << "copy"<<endl;
}
Vector(Vector&& v):size(v.size)
{
array = v.array;
v.array = nullptr;
cout << "move"<<endl;
}
~Vector()
{
delete array;
}
private:
int* array;
int size;
};
int main() {
Vector v(10);
Vector v1(std::move(v));
Vector v2(*(new Vector(2)));
Vector v3(Vector(2));
}
So why printing is not what I expected. Since I think both values passed to v2 and v3 are equal to Rvalue. And for v3, why does it print only Ctor without printing "move" or "copy"
source
share