This is the second page of Part 2.
Move semantics: Moving from Lvalue
Now, if you like to use a copy assignment function to implement your copy constructor, you may also try to implement the move constructor with the move copy assignment function. This is OK, but you have to be careful. The following is an error implementation:
C:\Temp>type unified_wrong.cpp
#include <stddef.h>
#include <iostream>
#include <ostream>
using namespace std;
class remote_integer {
Public
Remote_integer () {
cout << "Default constructor." << Endl;
m_p = NULL;
}
Explicit Remote_integer (const int n) {
cout << "unary constructor." << Endl;
m_p = new int (n);
}
Remote_integer (const remote_integer& Other) {
cout << "Copy constructor." << Endl;
m_p = NULL;
*this = other;
}
#ifdef movable
Remote_integer (remote_integer&& other) {
cout << "Move constructor." << Endl;
m_p = NULL;
*this = other; Wrong
}
#endif//#ifdef movable
remote_integer& operator= (const remote_integer& Other) {
cout << "Copy assignment operator." << Endl;
if (this!= &other) {
Delete m_p;
if (other.m_p) {
m_p = new int (*other.m_p);
} else {
m_p = NULL;
}
}
return *this;
}
#ifdef movable
remote_integer& operator= (remote_integer&& other) {
cout << "Move assignment OPERATOR." << Endl;
if (this!= &other) {
Delete m_p;
m_p = other.m_p;
other.m_p = NULL;
}
return *this;
}
#endif//#ifdef movable
~remote_integer () {
cout << "destructor." << Endl;
Delete m_p;
}
int get () const {
Return m_p? *m_p:0;
}