Overloading operator>> question

O

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?
 
M

Mike Wahler

Ook said:
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
 
M

Marcus Kwok

Ook said:
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;
 
O

Ook

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 :)
 
M

Mike Wahler

"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
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
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top