stream operator overloading question

Discussion in 'C++' started by Grey Alien, Aug 3, 2007.

  1. Grey Alien

    Grey Alien Guest

    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?
    Grey Alien, Aug 3, 2007
    #1
    1. Advertising

  2. Grey Alien

    Marcus Kwok Guest

    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?


    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
    Marcus Kwok, Aug 3, 2007
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Aug 3, 2007
    #3
  4. Grey Alien

    Grey Alien Guest

    Marcus Kwok wrote:

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

    >
    >
    > 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 ...
    Grey Alien, Aug 3, 2007
    #4
  5. Grey Alien

    Jerry Coffin Guest

    In article <>, 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.
    Jerry Coffin, Aug 4, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Magnusson
    Replies:
    0
    Views:
    385
    Martin Magnusson
    Aug 12, 2004
  2. Dietmar Kuehl
    Replies:
    0
    Views:
    446
    Dietmar Kuehl
    Aug 12, 2004
  3. John Smith
    Replies:
    2
    Views:
    417
    Ivan Vecerina
    Oct 6, 2004
  4. mrstephengross
    Replies:
    3
    Views:
    396
    James Kanze
    May 10, 2007
  5. VirGin
    Replies:
    6
    Views:
    666
    James Kanze
    Jan 22, 2008
Loading...

Share This Page