MyString S1, S2;//假设MyString类里有个char *类型的指针str 并且已经重载过运算符可以直接赋值字符串字面值 S1 = “this”; S2 = “that”; S1 = S2;
String & operator = (const String & s) { if(str == s.str) return * this; //防止 s = s ;出错 if(str) delete [] str; str = new char[strlen(s.str)+1]; strcpy(str , s.str); return * this; }
原文地址:http://blog.csdn.net/wangxiaobupt/article/details/37340477