Move constructors are special constructors that steal data from temporary object that are going be destroyed by the compiler.
Consider the following class:
class Point {
public:
Point(double x, double y);
~Point();
// Getters and setters here
void invalidate() {
x = nullptr;
y = nullptr;
}
private:
double *x {};
double *y {};
};
When a move is carried by using code that moves data from a temporary object using (rather contrived) code like the following:
Point p1(std::move(Point(10.5, 8.75)));
By default when a temporary object is moved, the compiler will use the copy constructor but this could be doing a lot of work depending on the class. Instead, we can simply grab the data and set any source pointers to nullptr. Consider the following example:
Point (Point &&source): x { source.get_x() }, y { source.get_y() } {
source.invalidate();
}
We get and invalidate the source data.