diff options
| -rw-r--r-- | main.cpp | 10 | ||||
| -rw-r--r-- | string.cpp | 37 | ||||
| -rw-r--r-- | string.h | 11 | 
3 files changed, 46 insertions, 12 deletions
| @@ -5,24 +5,24 @@ int main(int argc, char **argv)      MyString mystr("alpha");      if (mystr.isSubstring("ph"))      { -        cout << "true" << endl; +        std::cout << "true" << std::endl;      }      else -        cout << "false" << endl; +        std::cout << "false" << std::endl;      MyString mystr1 = ", beta";      MyString mystr2 = ", charlie";      MyString mystr3 = ", delta";      mystr.concat(mystr1).concat(mystr2).concat(mystr3); -    cout << mystr << endl; +    std::cout << mystr << std::endl;      MyString mystr4 = "hello";      mystr4.concat(", world!").concat(" goodbye"); -    cout << mystr4 << endl; +    std::cout << mystr4 << std::endl;      MyString mystr5 = "bro"; -    cout << mystr5 << endl; +    std::cout << mystr5 << ", char at 1 is: " << mystr5[1] << std::endl;      return 0;  } @@ -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; +} + @@ -8,12 +8,10 @@  #include <cstring>  #include <iostream> -using namespace std; -  class MyString  {      friend bool operator==(const MyString&, const MyString &); -    friend ostream& operator<<(ostream&, const MyString &); +    friend std::ostream& operator<<(std::ostream&, const MyString &);      private:          char *str_; @@ -36,13 +34,14 @@ class MyString          MyString& concat(const char *);          void printStr(void) const; -        MyString operator!() const; +        bool operator!(void) const;          char& operator[](int);          char operator[](int) const; +        operator char*(); /* outward class conversion into char * */  };  bool operator==(const MyString&, const MyString &); -ostream& operator<<(ostream&, const MyString &); +std::ostream& operator<<(std::ostream&, const MyString &);  inline MyString::MyString()  { @@ -66,7 +65,7 @@ inline int MyString::length() const  inline void MyString::printStr(void) const  { -    cout << "length: " << length_ << ", value: \"" << str_ <<"\""; +    std::cout << "length: " << length_ << ", value: \"" << str_ << "\"";  }  #endif | 
