#ifndef __STRING_RVALUE_HPP__ #define __STRING_RVALUE_HPP__ class String { private: char* ptr; int size; // size is the maximum size of the array (string) including '\0' public: String() { ptr = NULL; size = 0; } String(int s) { ptr = new char[s]; size = s; } String(String& str2); // copy constructor; lvalue reference; String(String&& str2); // move constructor; rvalue reference; String& operator= (String& str2); // overloading assignment operator; lvalue reference; String& operator= (String&& str2); // overloading move assignment operator; rvalue reference; }; // copy constructor; lvalue reference; String::String(String& str2) { size = str2.size; ptr = new char[size]; // allocate new space for (int i = 0; i < size; i++) ptr[i] = str2.ptr[i]; } // move constructor; rvalue reference; String::String(String&& str2) { size = str2.size; ptr = str2.ptr; // do not allocate new space str2.ptr = NULL; // important } // overloading assignment operator; lvalue reference; String& String::operator= (String& str2) { if (this != &str2) { if (ptr != NULL) delete[] ptr; size = str2.size; ptr = new char[size]; for (int i = 0; i < size; i++) ptr[i] = str2.ptr[i]; } return *this; } // overloading move assignment operator; rvalue reference; String& String::operator= (String&& str2) { if (this != &str2) { if (ptr != NULL) delete[] ptr; ptr = str2.ptr; size = str2.size; str2.ptr = NULL; } return *this; } #endif