我可以重载现有类中的现有函数/运算符吗?
我试图这样做:
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)