Re: IO-Operator overloading question

From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 07/10/04

  • Next message: James Dennett: "Re: inline assert [C++]"
    Date: 9 Jul 2004 21:45:53 -0700
    
    

    Francis Glassborow <francis@robinton.demon.co.uk> wrote in message news:<95zAgTWNVq7AFwnK@robinton.demon.co.uk>...

    [ ... ]

    > >class B {
    > > virtual std::ostream &write(std::ostream &os) {
    > > // code to write a B to a stream
    > > }
    > >
    > > friend std::ostream &operator<<(std::ostream &os, B const &b) {
    > > return b.write(os);
    > > }
    > >};
    > >
    > >class D : public D {
    > > virtual std::ostream &write(std::ostream &os) {
    > > // code to write a D to a stream.
    > > }
    > >};
    > >
    > >The invocation of operator<< itself isn't virtual, but all it does is
    > >invoke a virtual member function, which gives the same effect.
    >
    > And we have no need to involve a friend declaration.

    It CAN be written without a friend, by making the virtual I/O function
    ('write' in the code above) public. With 'write' private as it is
    above

    With 'write' being private as it is above, the 'friend' is needed --
    the code for operator<< is written inside of the class definition, but
    it's NOT really a class member -- the 'friend' declaration makes it a
    global even though its code is inside of the class definition.

    This is necessary, because as a member of B or D, something like
    'x<<y' would only resolve to using the function when the LEFT operand
    was of type B or D, which it will never be -- it'll always be (some
    descendant of) std::ostream.

    -- 
        Later,
        Jerry.
    The universe is a figment of its own imagination.
    

  • Next message: James Dennett: "Re: inline assert [C++]"

    Relevant Pages

    • TLL - into the archives 11 of 13
      ... I know who I am don't you worry about that and so does the police. ... unsolicited and I am not a member there. ... The exception is that family members of missing ...
      (alt.true-crime)
    • Re: How do I write to a textbox in a form from within a class
      ... you can control the level of access for each class member. ... Protected Friend ... Private PrivateVar As Integer ... to your TextBox issue ... ...
      (microsoft.public.dotnet.languages.vb)
    • Re: Templates and access checking
      ... Compilers report that data is private, ... > exposure that the friend function creates. ... A friend member function would be ... can have a member function that simply returns a value of 'data'. ...
      (comp.lang.cpp)
    • Re: Workplace Libel
      ... A friend at work has a problem: ... A member of staff deals with another member of staffon a daily ... professionalism in that is often very rude to in 's ... If a disciplinary meeting has been called, ...
      (uk.legal)
    • Re: When is a Quaker a Quaker?
      ... Quaker as the correct name for a member of the RSofF. ... I certainly do not denigrate the term "Friend". ... member of the Religious Society of Friends a.k.a. a Quaker. ... The *Church* ... ...
      (soc.religion.quaker)

    Loading