472,779 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

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

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
3 1980
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
1
by: PengYu.UT | last post by:
I heard that != >= > <= operators are defined based on the operator == <. Could you tell me which header file defines the four operators? Best wishes, Peng
9
by: Bill Yin | last post by:
what's mean?
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
2
sanjay123456
by: sanjay123456 | last post by:
Dear Friends, Plz tell me that How can i overload operator extraction and insertion in C++ sanjay
11
by: Holger | last post by:
Hi I have not been able to figure out how to do compound statement from C - "<test>?<true-val>:<false-val>" But something similar must exist...?! I would like to do the equivalent if python...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
2
by: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs & functions have to be in <scripttags. Thanks! ...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.