自定义string类中=赋值问题
mystring operator +(const mystring &str1, const mystring &str2){
mystring mystr3;
....
return mystr3;
}
mystring::mystring(const mystring &str2)//拷贝构造函数
{
length = str2.length;
str = new char [length + 1];
strcpy(str,str2.str);
}
void mystring::operator =(const mystring &str2)
{
.......
}
main函数中有s3 = s1 + s2;(mystring类型)
此语句先执行+,然后是拷贝构造函数,然后是=,但是其中在参数传值的过程我有些不明白,+ 完后返回的mystr3是拷贝构造函数参数的但是这时候mystr3应该是释放了啊,拷贝构造函数完了后的str也是传递给了=作参数.
我单步执行的.
赋值运算符系统默认的吗?什么时候应该自己重载.
string null_book = "9-999-99999-9";
To create null_book, the compiler first creates a temporary by invoking the string constructor that takes a cstyle character string parameter. The compiler then uses the string copy constructor to initialize null_book as a copy of that temporary.
那用这里 = 干嘛用啊,vod string::operator =(const string & ) 应该是在这里赋值的吧.
思维混乱中.