代码
#include <iostream>
#include <string>
using namespace std;
class Er {
public:
int e01;
string e02;
Er &operator>>(unsigned int x) {
this->e01 >>= x;
return *this;
}
Er &operator<<(unsigned int x) {
this->e01 <<= x;
return *this;
}
istream &operator<<(istream &is) {
is >> this->e01;
is >> this->e02;
return is;
}
};
ostream &operator<<(ostream &os, const Er &e) {
return os << '{' << e.e01 << ";" << e.e02 << '}';
}
int main() {
Er e = {2, "hello"};
/* 重载的移位运算符 */
e << 2;
cout << e << endl;
/* 重载的输入流运算 */
e << cin;
/* 重载的输出流运算 */
cout << e << endl;;
return 0;
}
适配重载运算符的查找规则
参考文献
C++程序设计语言第4版,458页