summaryrefslogtreecommitdiffstats
path: root/string.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'string.cpp')
-rw-r--r--string.cpp37
1 files changed, 36 insertions, 1 deletions
diff --git a/string.cpp b/string.cpp
index 393368f..42e97cc 100644
--- a/string.cpp
+++ b/string.cpp
@@ -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;
+}
+