Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef R_VALUE_REF
- #define R_VALUE_REF
- class String {
- public:
- String(String&&); // Move ctor
- String(const String&); // Normal copy ctor
- ~String(); // Dtor
- void swap(String&); //Constant time swap
- String& operator= (String&&); // Move assignment
- String& operator= (const String&); // Normal assignment
- private:
- int stringSize;
- char *str;
- };
- #endif // !R_VALUE_REF
- #include "class.hpp"
- String::String(const String& actual) { // Normal ctor
- stringSize = actual.stringSize;
- str = new char[stringSize];
- int i = 0;
- while (actual.str[i] != 0) {
- str[i] = actual.str[i];
- ++i;
- }
- str[i] = 0;
- }
- String::String(String&& actual) { // Move ctor
- stringSize = actual.stringSize;
- str = actual.str;
- actual.str = 0;
- actual.stringSize = 0;
- }
- String& String::operator=(const String& rhs) { // Normal assignment - Only called on l-values
- if (str == rhs.str) {
- return *this;
- }
- delete[] str;
- stringSize = rhs.stringSize;
- str = new char[stringSize];
- int i = 0;
- while (rhs.str[i] != 0) {
- str[i] = rhs.str[i];
- ++i;
- }
- str[i] = 0;
- return *this;
- }
- String& String::operator=(String&& rhs) { // Move assignment - Only called on r-values
- swap(rhs);
- return *this;
- }
- String::~String() {
- delete str;
- }
- void String::swap(String& rhs) { // Constant time swap
- char *tmp = str;
- str = rhs.str;
- rhs.str = tmp;
- int temp = stringSize;
- stringSize = rhs.stringSize;
- rhs.stringSize = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement