#ifndef __STRING_RVALUE__ #define __STRING_RVALUE__ #include class String { private: char* ptr; int stringSize; // number of chars in the string including '\0' public: String() { ptr = NULL; stringSize = 0; } String(char c) { ptr = new char[2]; ptr[0] = c; ptr[1] = '\0'; stringSize = 2; } String(const char* str, int size) { stringSize = size; ptr = new char[size]; for (int i = 0; i < size; i++) ptr[i] = str[i]; } // copy constructor // String(String& str2) // str2 -- lvalue reference { stringSize = str2.stringSize; ptr = new char[stringSize]; // allocate new resource for (int i = 0; i < stringSize; i++) { ptr[i] = str2.ptr[i]; } } // move constructor // String(String&& str2) // str2 -- rvalue reference { stringSize = str2.stringSize; // do not allocate new space for ptr array ptr = str2.ptr; str2.ptr = NULL; str2.stringSize = 0; } String & opertor=(String& str2) // str2 -- lvalue reference { stringSize = str2.stringSize; if (ptr == NULL) ptr = new char[stringSize]; // allocate new resource else { delete ptr; ptr = new char[stringSize]; // allocate new resource } for (int i = 0; i < stringSize; i++) { ptr[i] = str2.ptr[i]; } return *this; } // move assignment operator String& opertor = (String&& str2) // str2 -- rvalue reference { if (this != &str2) { stringSize = str2.stringSize; if (ptr != NULL) { delete ptr; ptr = NULL; } ptr = str2.ptr; str2.ptr = NULL; str2.stringSize = 0; } return *this; } }; #endif