Here is my code,
Could you point out the problems? Thanks for your inputs.Code:class MyString { public: MyString(const char* str) { m_str = new char[strlen(str)+1]; strcpy_s(m_str,strlen(str)+1, str); } MyString() { m_str = new char[1024]; } ~MyString() { delete[] m_str; } friend MyString operator+(MyString& a, const MyString& b); friend ostream& operator<<(ostream& os, MyString& str); private: char* m_str; }; MyString operator+(MyString& a, const MyString& b) { strcat_s(a.m_str, strlen(a.m_str) + strlen(b.m_str) + 1, b.m_str); return a.m_str; } ostream& operator<<(ostream& os, MyString& str) { os<<str.m_str; return os; } int _tmain(int argc, _TCHAR* argv[]) { MyString s1("Hi"); MyString s2; s2 = s1 + " there"; cout<<s2<<endl; return 0; }




Reply With Quote