Overloaded << and >> - Why Friend, Help

Discussion in 'C++' started by TJ, Apr 26, 2004.

  1. TJ

    TJ Guest

    Hi,

    I've been referring to many codes and books and always see that the
    stream insertion operators are overloaded as friends. Why is that?
    Are there any other overloading that need the same type of
    specification, as friends in the class that will use them?

    Thanks,

    TJ
    TJ, Apr 26, 2004
    #1
    1. Advertising

  2. TJ

    Ingo Nolden Guest

    Hi,

    if you can not efficiently extract the data to be inserted from the class which is on the right sode of the op from
    public members you need to access the private ones. In this case you need to be the class' friend.



    TJ <>
    wrote in message: <>

    >Hi,
    >
    >I've been referring to many codes and books and always see that the
    >stream insertion operators are overloaded as friends. Why is that?
    >Are there any other overloading that need the same type of
    >specification, as friends in the class that will use them?
    >
    >Thanks,
    >
    >
    Ingo Nolden, Apr 26, 2004
    #2
    1. Advertising

  3. TJ

    Andre Kostur Guest

    TJ <> wrote in news::

    > Hi,
    >
    > I've been referring to many codes and books and always see that the
    > stream insertion operators are overloaded as friends. Why is that?
    > Are there any other overloading that need the same type of
    > specification, as friends in the class that will use them?


    It's not strictly required. What's probably happening is that the
    streaming operators require access to private members of the class in order
    to stream that class in/out.
    Andre Kostur, Apr 26, 2004
    #3
  4. TJ

    David Harmon Guest

    On Mon, 26 Apr 2004 15:43:20 -0400 in comp.lang.c++, TJ <> wrote,
    >I've been referring to many codes and books and always see that the
    >stream insertion operators are overloaded as friends. Why is that?


    This and several related issues are covered in section 15 of Marshall
    Cline's C++ FAQ. It is always good to check the FAQ before posting.
    You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/

    I disagree quite a bit with the FAQ on this. I like the public member
    "printon()" function and standalone non-friend inline operator>>().
    printon() may or may not be virtual, as your requirements dictate, but
    most often it probably is. The claim that people are not supposed to
    call printon() is baloney.

    Take your choice.
    David Harmon, Apr 26, 2004
    #4
    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. Nitin Bhardwaj
    Replies:
    8
    Views:
    843
    Andrey Tarasevich
    Jul 14, 2003
  2. CoolPint
    Replies:
    3
    Views:
    479
    Victor Bazarov
    Feb 13, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,735
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    4
    Views:
    472
  5. Peter
    Replies:
    2
    Views:
    264
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page