class mystring {
private:
string s;
public:
mystring(string ss) {
cout << "mystring : mystring() : " + s <<endl;
s = ss;
}
/*! mystring& operator=(const string ss) {
cout << "mystring : mystring& operator=(string) : " + s <<endl;
s = ss;
//! return this;
return (mystring&)this; // why COMPILE ERROR
} */
mystring operator=(const string ss) {
cout << "mystring : mystring operator=(string) : " + s <<endl;
s = ss;
return *this;
}
mystring operator=(const char ss[]) {
cout << "mystring : mystring operator=(char[]) : " << ss <<endl;
s = ss;
return *this;
}
};
mystring str1 = "abc"; // why COMPILE ERROR
mystring *str2 = new mystring("bcd");
所以问题是
如何进行正确的 mystring& opeartor= 重载?也就是说,我怎样才能返回引用而不是指针?(我们可以在 C++ 中的引用和指针之间转移吗?)
如何使正确的 mystring operator= 重载?我认为源代码可以正常工作,但事实证明我仍然无法将 const char[] 分配给 mystring,就好像我没有重载 operator= 一样。
谢谢。