Member operators operator>>() and operator<<() in a program below
work fine, but look strange.
Is it possible to define member operators operator>>() and operator<<()
that work fine and look fine?
// --------- foo.cpp ---------
#include <iostream>
using namespace std;
class Foo
{
private:
int data_;
public:
istream& operator>>(istream& is_o);
ostream& operator<<(ostream& os_o);
};
// -------------
istream& Foo::operator>> (istream& is_o)
{
return is_o >> data_;
}
// -------------
ostream& Foo::operator<<(ostream& os_o)
{
return os_o << data_ << endl;
}
// -------------
int main()
{
Foo foo;
foo.operator>>(cin);
foo.operator<<(cout);
foo >> cin; // Works fine, but looks weird
foo << cout; // -------- the same ---------
// cin >> foo; // Of course invalid in for class Foo
// cout << foo; // ------------ the same ------------
return 0;
}
// ---------------------------
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn