我有简单的设置:
#include<iostream>
class Stuff {};
ostream &operator<<(ostream &lhs, const Stuff &rhs) {
return lhs << "something";
}
int main() {
Stuff stuff;
cout << stuff << endl;
cin.get();
}
该函数将模型类operator<<
打印到. 我想做的就是将该函数移到类本身中。如:Stuff
ostream
Stuff
class Stuff {
ostream &operator<<(ostream &lhs, const Stuff &rhs) {
return lhs << "something";
}
};
不过,对于我的生活,我无法弄清楚如何让它发挥作用。我感觉到我正在尝试从右侧重新定义左关联运算符。有没有办法正确地做到这一点?