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

A

Alex Vinokur

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::eek:perator>> (istream& is_o)
{
return is_o >> data_;
}


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

ostream& Foo::eek: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;
}
// ---------------------------
 
J

John Carson

Alex Vinokur said:
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.
 
R

Rayer

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::eek:perator>> (istream& is_o)
{
return is_o >> data_;
}


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

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

ostream &Foo::eek: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.
 
J

Jeff Schwab

Alex said:
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::eek:perator>> (istream& is_o)
{
return is_o >> data_;
}


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

ostream& Foo::eek: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top