#include<iostream>
using namespce std;
class String
{
public:
String()
:_str(new char[1])
{
_str='\0';
}
String(const char* str)//一个字符串的拷贝
:_str(new char[strlen(str)+1]
{
strcpy(_str,str);
}
String (const String&str)//一个对象的拷贝
:_str(NULL)
{
String tmp=str._str;//调用了拷贝构造函数生成了一个String类的对象
swap(_str,tmp._str);
}
/*String (const String&str)//一个对象的拷贝
:_str(new char[strlen(str._str)+1)
{
strcpy(_str,str._str);
}*/
String &operator=(String str)//直接调用拷贝构造函数生成str
{
swap(_str,str._str);
return *this;
}
/*String &operator=(const String &str)//书上常用的方法
{
if(this==&str)
{
return *this;
}
delete _str;
_str=NULL;
_str=new char[strlen(str._str)+1];
strcpy(_str,str._str);
return *this;
}*/
private:
char*_str;
};

以上方法是最简单实现string类的方法,也有其它的方法!