读《《effective C++ 》》
构造函数: 初始化,检验参数的有效性
赋值函数: 必须释放原有的资源
构造函数:
String:: String(const char *value)
{
if(value){
data = new char[strlen(rhs.data)+1];
strcpy(data,rhs.data);
}
else
{
data = new char[1];
*data=`\0';
}
}
赋值函数
String& String:: operation=(const String& rhs)
{
if(this == &rhs)
return *this;
delete [ ] data;// free resource of old memory
data = new char[strlen(rhs.data)+1];
strcpy(data,rhs.data);
return *this;
}