我正在尝试制作一个控制台类。我想将cinand包装cout在类中并重载<<and>>运算符。所以我可以这样使用这个类:
// Output
Console << "Call cout from Console" << endl;
// Input
string str;
Console >> str; // Call cin
我最好的猜测是:
class Console {
//...
public:
ostream& operator<< (ostream& os)
{
cout << os;
return &cout;
}
//...
};
但我知道那是错误的,我怎么能重载运算符以将 Console 类用作cinand cout?