diff options
1 files changed, 47 insertions, 2 deletions
| -rw-r--r-- | src/String/String.cpp | 49 |
diff --git a/src/String/String.cpp b/src/String/String.cpp index f94dce2..ee9c9df 100644 --- a/src/String/String.cpp +++ b/src/String/String.cpp @@ -8,6 +8,51 @@ void String::free() { } void String::copyFrom(const String& other) { - this->str = new char[other.length]; - strcpy(this->str, other.str); + str = new char[other.length]; + strcpy(str, other.str); + length = other.length; +} + +void String::moveFrom(String&& other) { + str = other.str; + other.str = nullptr; + length = other.length; +} + +/* Public */ + +String::String(const char* str) { + length = strlen(str) + 1; // capture terminating zero + this->str = new char[length]; + strcpy(this->str, str); +} + +String::String() : String("") {} + +String::String(const String& other) { + copyFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +String::~String() { + free(); +} + +String::String(String&& other) { + moveFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + moveFrom(other); + } + return *this; } |
