diff options
Diffstat (limited to 'string.cpp')
-rw-r--r-- | string.cpp | 37 |
1 files changed, 36 insertions, 1 deletions
@@ -139,8 +139,43 @@ bool operator==(const MyString& lhs, const MyString& rhs) * but it seems like the & is placed there so we can make consecutive overloaded * calls to cout, effectively chaining all of the calls */ -ostream& operator<<(ostream& ostr, const MyString& 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 */ +std::ostream& operator<<(std::ostream& ostr, const MyString& rhs) { return (ostr << rhs.str_); } +/* unary operator operate on one operand, itself! */ +bool MyString::operator!(void) const +{ + return (this->length_ == 0); +} + +char& MyString::operator[](int i) +{ + /* references need to point to valid objects, all hell could breaks loose */ + return this->str_[i]; +} + +char MyString::operator[](int i) const +{ + if (i < 0 || i > this->length_ - 1) + return 0; + else + return this->str_[i]; +} + +MyString::operator char*() +{ + char *ret = new char[this->length_ + 1]; + if (ret) + { + strcpy(ret, this->str_); + return ret; + } + else + return NULL; +} + |