class string
{
char *m_str;
public:
string(char *s)
{
m_str=s;
}
string()
{};
String & operator=(const string s) // 这里!!!返回值为啥是引用
{
m_str=s.m_str;
return *this
}// 返回*this不是会调用析构函数的吗!
};
int main()
{
string s1("abc"),s2;
s2=s1;
cout<<s2.m_str;
}
为了防止不必要的值被复制。如果返回值,那么会把值都复制一遍再传到外面;返回引用的话,返回的对象还是与*this同一个地址,值就不会被复制了。
如果有帮助,希望采纳,谢谢