我可以重载现有类中的现有函数/运算符吗?
我想做的是:
#include <iostream>
#include <string>
using namespace std;
string& string::operator<<(const string& str) {
this->append(str);
}
但这给了我错误:
test.cpp:5: error: too few template-parameter-lists
我该怎么做?或者我不能?
最佳答案
除非您修改该类的定义,否则您不能向该类添加成员函数。请改用免费功能:
string& operator<<(string & lhs, const string & rhs) {
return lhs += rhs;
}
关于C++:字符串运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11515925/