Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stream operator overloading question

Reply
Thread Tools

stream operator overloading question

 
 
Grey Alien
Guest
Posts: n/a
 
      08-03-2007
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?
 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      08-03-2007
Grey Alien <(E-Mail Removed)> wrote:
> 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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2007
Grey Alien wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Grey Alien
Guest
Posts: n/a
 
      08-03-2007


Marcus Kwok wrote:

> Grey Alien <(E-Mail Removed)> wrote:
>
>>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.
>


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 ...
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      08-04-2007
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stream Operator Overloading Design Choices VirGin C++ 6 01-22-2008 01:25 PM
Stream operator in namespace masks global stream operator mrstephengross C++ 3 05-10-2007 07:02 AM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Re: Disabled out stream (operator overloading problem) Dietmar Kuehl C++ 0 08-12-2004 12:18 PM
Disabled out stream (operator overloading problem) Martin Magnusson C++ 0 08-12-2004 11:40 AM



Advertisments