diff options
Diffstat (limited to 'week06/Exercise02.cpp')
| -rw-r--r-- | week06/Exercise02.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/week06/Exercise02.cpp b/week06/Exercise02.cpp new file mode 100644 index 0000000..63daeac --- /dev/null +++ b/week06/Exercise02.cpp @@ -0,0 +1,35 @@ +#include <cstring> + +struct DynamicString { +private: + char* str; + unsigned size; + + void free() { + delete[] str; + } + void copyFrom(const DynamicString& other) { + this->str = new char[other.size]; + strcpy(this->str, other.str); + this->size = other.size; + } + +public: + DynamicString() { + str = nullptr; + size = 0; + } + ~DynamicString() { + free(); + } + DynamicString(const DynamicString& other) { + copyFrom(other); + } + DynamicString& operator=(const DynamicString& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; + } +}; |
