By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

Member operators operator>>() and operator<<()

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Alex Vinokur" <al****@x-privat.org> wrote in message
news:42********@x-privat.org
Member operators operator>>() and operator<<() in a program below
work fine, but look strange.


Simple (and usual) solution. Don't use member operators. Use friends of the
class instead.
--
John Carson

Jul 23 '05 #2

P: n/a
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;
} I cant sure weather you look be fine, but how about this?
istream &Foo::operator>>(istream &is_o)
{
iso_o >> data_;
return iso_o;
}

ostream &Foo::operator<<(ostream &os_o)
{
os_o << data_ << endl;
return os_o;
}

I think it will express better, and how do you think 'bout it?
// -------------
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;
}
// ---------------------------

I think this problem is because you have not use friend function in this
class, try add these in your class

friend ostream &operator>>(ostream &os_o, Foo &ref);
friend istream &operator<<(istream &is_o, Foo &ref);

and have these functions defined

ostream &operator<<(ostream &os_o, Foo &ref)
{
os_o << ref.data_;
return os_o;
}

istream &operator>>(istream &is_o, Foo &ref)
{
is_o >> ref.data_;
return is_o;
}

hope these can slove your problem, and I think
cin >> foo;
cout << foo;
will work now.

the problem is, if you define operator>> and operator<< in a class,
these will be see as operator>>(class typedef, iostream), so if you
don't wanna use such as foo>>cin nor foo<<cout, you surely shouldn't
define such like those operator in a class. Instead, you should define
those by friend function.
Jul 23 '05 #3

P: n/a
Alex Vinokur wrote:
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;
}
// ---------------------------


I don't think that looks particularly weird. If it makes you feel
better, give Foo public set_data( int ) and get_data( ) methods, and use
non-member operator>> and operator<< to call set_data and get_data.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.