我正在做一个小程序来模拟使用基本字符串。它目前无法始终如一地工作。
在这种情况下,程序工作正常:
a = a+ w + 10 + " " + L"x" + 65.5 ;
但是,如果我以这种方式写相同的句子,所有工作都不好:
a = + w + 10 + " " + L"x" + 65.5 ;
谁能向我解释我的程序有什么问题?
class sstring {
public:
string s;
sstring() {s.assign("");}
template <class T>
sstring& operator=(T i) {
s = to_string( i );
return *this;
}
sstring& operator=(const char *i) {
s = i;
return *this;
}
sstring& operator=(const wchar_t *w) {
wstring ws = w;
s.assign ( ws.begin(),ws.end() );
return *this;
}
sstring& operator=(wstring w) {
s.assign ( w.begin(),w.end() );
return *this;
}
// *********************************************** +
template <class T>
sstring& operator+(T i) {
s.append( to_string( i ));
return *this;
}
sstring& operator+(const char *i) {
s.append(i);
return *this;
}
sstring& operator+(const wchar_t *i) {
wstring ws = i;
ws.assign(i);
string cs;
cs.assign ( ws.begin(),ws.end() );
s.append( cs );
return *this;
}
sstring& operator+(wstring w) {
string temp;
temp.assign( w.begin(),w.end() );
s.append ( temp );
return *this;
}
//*************************************************** <<
friend ostream& operator<<( ostream &out,sstring obj);
};
ostream& operator<<( ostream &out,sstring obj) {
out << obj.s;
return out;
}
int main(void) {
sstring a;
wstring w;
w = L"claudio";
a = "daffra";
a = a + w + 10 + " " + L"x" + 65.5;
cout << "\ns :" << a;
return 1;
}