#include #include using std::ostream; using std::cout; using std::endl; class string { public: string() : buf( new char[1]) { cout << "default" << endl; buf[0] = '\0'; } string(const char* s) : buf(new char[strlen(s)+1]) { cout << "convert" << endl; strcpy(buf, s); } string(const string& s) : buf(new char[s.size()+1]) { cout << "copy" << endl; strcpy(buf, s.buf); } int size() const { return strlen(buf); } const char* getBuf() const { return buf; } void setBuf(const char* b) { delete [] buf; buf = new char[strlen(b)+1]; strcpy(buf, b); } const string& operator=(const string& rhs) { cout << "assign" << endl; if (&rhs != this) { setBuf(rhs.buf); } return *this; } private: char *buf; }; ostream& operator<<(ostream &lhs, const string &s) { return lhs << s.getBuf(); } class Test { } int main() { string x; string dog("dog"); string cat = dog; x = cat; }