#include <iostream>
#include <vector>
using namespace std;
class X
{
public:
std::vector<double> data;
// Constructor1
X():
data(100000) // lots of data
{
cout << "X() called" << endl;
}
// Constructor2
X(X const& other) // copy constructor
: data(other.data) // duplicate all that data
{
cout << "X(X const& other) called" << endl;
}
// Constructor3
X(X&& other): // move constructor
data(std::move(other.data)) // move the data: no copies
{
cout << "X(X&& other) called" << endl;
}
X& operator=(X const& other) // copy-assignment
{
cout << "X& operator=(X const& other) called" << endl;
data=other.data; // copy all the data
return *this;
}
X& operator=(X && other) // move-assignment
{
cout << "X& operator=(X && other) called" << endl;
data=std::move(other.data); // move the data: no copies
return *this;
}
};
class X2
{
public:
std::vector<double> data;
// Constructor1
X2()
: data(100000) // lots of data
{
cout << "X2() called" << endl;
}
// Constructor2
X2(X const& other)
: data(other.data)
{
cout << "X2(const X&) called" << endl;
}
X2& operator=(X const& other) // copy-assignment
{
data=other.data; // copy all the data
cout << "X2::operator =(const X&) called" << endl;
return *this;
}
};
X make_x()
{
X myNewObject;
myNewObject.data.push_back(22);
return myNewObject;
}
int main()
{
X x = make_x(); // x1 has a move constructor
X2 x2 = make_x(); // x2 hasn't a move constructor
return 0;
}