stream operator overloading question

M

Marcus Kwok

Grey Alien said:
I recently read this:

"The best way to overload the stream operators is not to make them
members of any class, but to keep them as friends." (source:
http://www.codersource.net/cpp_stream_operators.html)

Is that true (I can't see why should be the case). If it is true, could
someone explain why?

Well, the idiomatic way to use the stream operators is like:

in >> x;

or

out << x;

In both cases, in order to make them members, they would need to be
members of the stream classes, not your own class. In other words, it
would have to be like:

class istream {
public:
istream& operator>>(X& x);
};

class ostream {
public:
ostream& operator<<(const X& x);
};

However, you are not allowed to modify the std stream classes like that.
Therefore, it is better to make them free functions:

istream& operator>>(istream& i, X& x);

ostream& operator<<(ostream& o, const X& x);

and if they need access to the class's internals, then they are made
friends.
 
V

Victor Bazarov

Grey said:
I recently read this:

"The best way to overload the stream operators is not to make them
members of any class, but to keep them as friends." (source:
http://www.codersource.net/cpp_stream_operators.html)

Is that true (I can't see why should be the case). If it is true,
could someone explain why?

If you want to use operator<< or operator>> with your type, what's
on the left side of the operator? What type then should those
operators be members of? You can, of course, reverse the operands
and overload those operands that way, what's the syntax is going
to be? Try implementing all those to see how it feels. (I am not
going to comment on the use of 'void main' in the program on the
page link to which you gave)

V
 
G

Grey Alien

Marcus said:
Well, the idiomatic way to use the stream operators is like:

in >> x;

or

out << x;

In both cases, in order to make them members, they would need to be
members of the stream classes, not your own class. In other words, it
would have to be like:

class istream {
public:
istream& operator>>(X& x);
};

class ostream {
public:
ostream& operator<<(const X& x);
};

However, you are not allowed to modify the std stream classes like that.
Therefore, it is better to make them free functions:

istream& operator>>(istream& i, X& x);

ostream& operator<<(ostream& o, const X& x);

and if they need access to the class's internals, then they are made
friends.

Thanks for the lucid clarification - and also, for realizing that we not
all born with an implicit, detailed knowledge of C++. Some of us are
still learning ...
 
J

Jerry Coffin

I recently read this:

"The best way to overload the stream operators is not to make them
members of any class, but to keep them as friends." (source:
http://www.codersource.net/cpp_stream_operators.html)

Is that true (I can't see why should be the case). If it is true, could
someone explain why?

Because their left operand is normally a stream object, so as members
they'd have to be members of the stream class. If they weren't free
functions, you'd need to constantly modify the stream class to make that
happen.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top