Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Member operators operator>>() and operator<<()

Reply
Thread Tools

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

 
 
Alex Vinokur
Guest
Posts: n/a
 
      03-20-2005
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:perator>> (istream& is_o)
{
return is_o >> data_;
}


// -------------

ostream& Foo:perator<<(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





 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      03-20-2005
"Alex Vinokur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> 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

 
Reply With Quote
 
 
 
 
Rayer
Guest
Posts: n/a
 
      03-20-2005

> 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:perator>> (istream& is_o)
> {
> return is_o >> data_;
> }
>
>
> // -------------
>
> ostream& Foo:perator<<(ostream& os_o)
> {
> return os_o << data_ << endl;
> }

I cant sure weather you look be fine, but how about this?
istream &Foo:perator>>(istream &is_o)
{
iso_o >> data_;
return iso_o;
}

ostream &Foo:perator<<(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.
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      03-20-2005
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:perator>> (istream& is_o)
> {
> return is_o >> data_;
> }
>
>
> // -------------
>
> ostream& Foo:perator<<(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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-member operators saneman C++ 1 02-15-2008 10:36 PM
can member operators be virutal? does it make sense? puzzlecracker C++ 2 08-19-2007 09:29 PM
Template member operators. Alan Woodland C++ 3 09-14-2006 05:17 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
friend vs member for comparison operators Michael Klatt C++ 4 10-21-2003 02:18 AM



Advertisments