【发布时间】:2014-08-30 20:27:49
【问题描述】:
为什么为std::string 定义了operator+= 而没有定义operator+?请参阅下面的 MWE (http://ideone.com/OWQsJk)。
#include <iostream>
#include <string>
using namespace std;
int main() {
string first;
first = "Day";
first += "number";
cout << "\nfirst = " << first << endl;
string second;
//second = "abc" + "def"; // This won't compile
cout << "\nsecond = " << second << endl;
return 0;
}
【问题讨论】:
-
您希望:
"abc"->operator+("def")工作? -
@crashmstr 好吧,我明白他们为什么会这样做。它会在许多其他语言中(也就是说,字符串文字是类类型)。
标签: c++ string c++11 operators