473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Overloading operator>> question

Ook
I'm a but fuzzy about this. You would call it like this:Polynomial poly; //
Polynomial is my class with various properties
cin >> poly;

In the function itself, I would, for example, do the following:

istream& operator>>(istream& is, Polynomial& poly)
{
is >> poly._size;
is >> poly._coefficient[1];
return is;
}

Since we are passing poly by reference, I can modify it's properties
directly. This is where I loose it. If I'm modifying the properties of
poly directly in the overloaded function, why do I need to "return is"?
What exactly happens when you return from this function, and am I on the
right track about modifying the properties of poly in the function itself?
Oct 14 '05 #1
4 1523
"Ook" <Don't send me any freakin' spam> wrote in message
news:JK******************************@giganews.com ...
I'm a but fuzzy about this. You would call it like this:Polynomial poly;
// Polynomial is my class with various properties
cin >> poly;

In the function itself, I would, for example, do the following:

istream& operator>>(istream& is, Polynomial& poly)
{
is >> poly._size;
is >> poly._coefficient[1];
return is;
}

Since we are passing poly by reference, I can modify it's properties
directly. This is where I loose it. If I'm modifying the properties of
poly directly in the overloaded function, why do I need to "return is"?
You don't. But it's almost always recommended that you do, so that
the '>>' calls can be chained, e.g.

std::cin >> poly1 >> poly2;

... as we're accustomed to doing with e.g. 'int' objects.
What exactly happens when you return from this function,
Nothing 'happens', it's simply that a reference to the
stream is being returned. The caller is free to use it
or not. I'd always advise the caller does use it, if
for no other reason that to check for errors, e.g.

if(!(std::cin >> poly1))
std::cerr << "input error\n";
and am I on the
right track about modifying the properties of poly in the function itself?
Yes, that's normally the way it's done. But since typically those
'attributes', e.g. '_size' and '_coefficient' would be private,
common practice is to make your operator>> function a friend
of the class. Another way would be to make it not a friend,
and have it call a public function which would actually implement
the stream operation.

-Mike
Mike

Oct 14 '05 #2
Ook <Don't send me any freakin' spam> wrote:
I'm a but fuzzy about this. You would call it like this:Polynomial poly; //
Polynomial is my class with various properties
cin >> poly;

In the function itself, I would, for example, do the following:

istream& operator>>(istream& is, Polynomial& poly)
{
is >> poly._size;
is >> poly._coefficient[1];
return is;
}

Since we are passing poly by reference, I can modify it's properties
directly. This is where I loose it. If I'm modifying the properties of
poly directly in the overloaded function, why do I need to "return is"?
What exactly happens when you return from this function, and am I on the
right track about modifying the properties of poly in the function itself?


That sounds fine to me. The reason for returning "is" is so that you
can chain together >> commands:

std::ifstream in_file("whatever.txt");
Polynomial p1;
Polynomial p2;
int i;

in_file >> p1 >> p2 >> i;

--
Marcus Kwok
Oct 14 '05 #3
Ook

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:wy**************@newsread2.news.pas.earthlink .net...
<snip>

Yes, that's normally the way it's done. But since typically those
'attributes', e.g. '_size' and '_coefficient' would be private,
common practice is to make your operator>> function a friend
of the class. Another way would be to make it not a friend,
and have it call a public function which would actually implement
the stream operation.

-Mike
Mike


holy moly, 5 minutes and there is an answer here! I'm taking a Data
Structures class, and I gotta say, this NG is the best place to go when I'm
stumped and can't google/search the answer to a problem!

Actually, I do declare it as friend, and I'm glad I'm on the right track. I
can usually figure these things out, but I'm not always sure that the answer
I come up with is right, and getting the syntax just right is still hard for
me. Just out of curiosity, how would I go about using the reference to the
stream that is returned? I don't think I have ever done so because I do
everything I need to in the overloaded function itself. And tnx for the fast
response :)
Oct 14 '05 #4

"Ook" <Ook Don't send me any freakin' spam at zootal dot com delete the
Don't send me any freakin' spam> wrote in message
news:xZ********************@giganews.com...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:wy**************@newsread2.news.pas.earthlink .net...
<snip>

Yes, that's normally the way it's done. But since typically those
'attributes', e.g. '_size' and '_coefficient' would be private,
common practice is to make your operator>> function a friend
of the class. Another way would be to make it not a friend,
and have it call a public function which would actually implement
the stream operation.

-Mike
Mike
holy moly, 5 minutes and there is an answer here!


Many folks from all over the world ask and answer
questions here. It's just random chance that mine
was the first answer you saw. Also, don't take my
word as 'gospel', I'm human, and have been known
to make errors. :-) And even if my answer is perfectly
correct, it's always a good idea to read other replies
as well, to get different perspectives (and corrections
to those who err).
I'm taking a Data Structures class, and I gotta say, this NG is the best
place to go when I'm stumped and can't google/search the answer to a
problem!
Yes, I learn much here as well.
Actually, I do declare it as friend, and I'm glad I'm on the right track.
I can usually figure these things out, but I'm not always sure that the
answer I come up with is right, and getting the syntax just right is still
hard for me. Just out of curiosity, how would I go about using the
reference to the stream that is returned?

I already showed you:

if(!(std::cin >> poly1))
std::cerr << "input error\n";

The expression 'std::cin >> poly1' returns a reference
to 'std::cin'. It is 'used' here by evaluating its value
in a boolean context (with the ! operator).
I don't think I have ever done so because I do everything I need to in the
overloaded function itself.
Except the most important part: checking for errors. :-)
(If the operation had failed, the stream would go into
a 'fail' state, and *any* subsequent i/o requests would
also fail. Let this problem 'cascade' many levels of
function calls, and you'd have no idea where the problem
was.)

*Always* check for errors, *especially* with i/o, and
*more* especially when the input comes from a user.
And tnx for the fast response :)


You're welcome.

-Mike
Oct 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
4
by: Don Hedgpeth | last post by:
Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as. //code for class1 friend std::istream& operator >> (std::istream&...
6
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
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...
11
by: Ashwin | last post by:
hi guys, i have overloaded >operator for my own string class .the function is as given below istream& operator>>(istream &in,string1 &s) { char *a; a=new char; in>>a; s=a;
0
by: rama270 | last post by:
Problem: The operator -> is overloaded thus Class * operator-> () { return this; } but this causes a problem for const member functions . The purpose of overloading is that both . and -> can be...
2
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly...
3
by: Demoris | last post by:
I have a class that I believe should work. I've compared it to previous programs I've written, compared it to examples from my professor, to examples in my textbook, but can't see why I get the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.