diff options
Diffstat (limited to 'string.cpp')
-rw-r--r-- | string.cpp | 61 |
1 files changed, 39 insertions, 22 deletions
@@ -9,23 +9,41 @@ MyString::MyString(int len) : str_(new char[len + 1]), length_(len) { + this->count++; } -MyString::MyString(const char *str) : length_(strlen(str)) +MyString::MyString(const char *str) : str_(new char[strlen(str)+1]), length_(strlen(str)) { - str_ = new char[length_ + 1]; strcpy(str_, str); + this->count++; } MyString::MyString(const MyString& str) : str_(new char[str.length_ + 1]), length_(str.length_) { strcpy(str_, str.str_); + this->count++; +} + +MyString& MyString::operator=(const MyString& rhs) +{ + /* don't do anything if object assigns itself */ + if (this == &rhs) + return *this; + + length_ = rhs.length_; + delete[] str_; + str_ = new char[ length_ + 1]; + strcpy(str_, rhs.str_); + + /* assignment operator requires this */ + return *this; } MyString::~MyString() { delete[] str_; + this->count--; } int MyString::indexOf(char ch, int pos) const @@ -41,6 +59,9 @@ int MyString::indexOf(char ch, int pos) const return -1; } +/* define this field only once, .h is not the best place */ +size_t MyString::count = 0; + bool MyString::isSubstring(const MyString& str) const { if (length_ < str.length_) @@ -65,6 +86,9 @@ bool MyString::isSubstring(const MyString& str) const return false; } +/* might seem redundant since constructor can create an object from char array + * and call other isSubstring method that takes in MyString type + */ bool MyString::isSubstring(const char *str) const { int i, j, k; @@ -109,21 +133,6 @@ MyString& MyString::concat(const char *str) return *this; } -MyString& MyString::operator=(const MyString& rhs) -{ - /* don't do anything if object assigns itself */ - if (this == &rhs) - return *this; - - length_ = rhs.length_; - delete[] str_; - str_ = new char[ length_ + 1]; - strcpy(str_, rhs.str_); - - /* assignment operator requires this */ - return *this; -} - bool operator==(const MyString& lhs, const MyString& rhs) { if (lhs.length_ != rhs.length_) @@ -135,13 +144,17 @@ bool operator==(const MyString& lhs, const MyString& rhs) return false; } -/* seems like the ostream& in front has nothing to do with dereferencing a pointer, - * but it seems like the & is placed there so we can make consecutive overloaded - * calls to cout, effectively chaining all of the calls - */ +bool operator!=(const MyString& lhs, const MyString& rhs) +{ + return !(lhs == rhs); +} /* right now I think the reference is used to avoid returning copy of ostream - * so when calls are chained up we work on one ostream object only */ + * so when calls are chained up we work on one ostream object only + * + * this function is in global scope, it is friend'ed to allow us access to data + * members of calss MyString + */ std::ostream& operator<<(std::ostream& ostr, const MyString& rhs) { return (ostr << rhs.str_); @@ -179,3 +192,7 @@ MyString::operator char*() return NULL; } +MyString::operator const char*() const +{ + return this->str_; +} |