my_string class
ref_cnt
class my_string { public: my_string() { st = new str_obj; } my_string(const char* p) {st = new str_obj(p);} my_string(const my_string& str) { st = str.st; st -> ref_cnt++; } ~my_string(); void assign(const my_string& str); void print() const { cout << st -> s; } private: str_obj* st; }; void my_string::assign(const my_string& str) { if (str.st != st) { if (--st -> ref_cnt == 0) delete st; st = str.st; st -> ref_cnt++; } } my_string:: ~my_string() { if (--st -> ref_cnt == 0) delete st; }