Re: templated operator()<<

Discussion in 'C++' started by Alf P. Steinbach /Usenet, Apr 12, 2011.

  1. * Ruben Safir, on 12.04.2011 01:39:
    > as I understand things, the operator<<() needs to be external as to the
    > class and then declared as a friend of the class. I'm not sure if I
    > understand why that is the case.


    It's not the case. ;-)


    [snip question that's apparently already answered]

    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Apr 12, 2011
    #1
    1. Advertising

  2. * Ruben Safir, on 12.04.2011 03:59:
    > On Tue, 12 Apr 2011 02:29:10 +0200, Alf P. Steinbach /Usenet wrote:
    >
    >> * Ruben Safir, on 12.04.2011 01:39:
    >>> as I understand things, the operator<<() needs to be external as to the
    >>> class and then declared as a friend of the class. I'm not sure if I
    >>> understand why that is the case.

    >>

    >
    >
    > what?? operator<<() can be a normal class member?


    Yes.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Apr 12, 2011
    #2
    1. Advertising

  3. * Ruben Safir, on 12.04.2011 04:43:
    > On Tue, 12 Apr 2011 04:01:25 +0200, Alf P. Steinbach /Usenet wrote:
    >
    >> * Ruben Safir, on 12.04.2011 03:59:
    >>> On Tue, 12 Apr 2011 02:29:10 +0200, Alf P. Steinbach /Usenet wrote:
    >>>
    >>>> * Ruben Safir, on 12.04.2011 01:39:
    >>>>> as I understand things, the operator<<() needs to be external as to
    >>>>> the class and then declared as a friend of the class. I'm not sure
    >>>>> if I understand why that is the case.
    >>>>
    >>>>
    >>>
    >>> what?? operator<<() can be a normal class member?

    >>
    >> Yes.
    >>
    >>
    >> Cheers& hth.,
    >>
    >> - Alf

    > http://en.wikibooks.org/wiki/C _Pr...tor_Overloading#Operators_as_member_functions
    >
    >
    > Of special mention are the shift operators,<< and>>. These have been overloaded in the standard library for interaction with streams. When overloading these operators to work with streams the rules below should be followed:
    >
    > 1. overload<< and>> as friends (so that it can access the private variables with the stream be passed in by references
    > 2. (input/output modifies the stream, and copying is not allowed)
    > 3. the operator should return a reference to the stream it receives (to allow chaining, cout<< 3<< 4<< 5)


    Your question seems to be missing.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Apr 12, 2011
    #3
  4. Alf P. Steinbach /Usenet

    Kai-Uwe Bux Guest

    Ruben Safir wrote:

    > On Tue, 12 Apr 2011 02:29:10 +0200, Alf P. Steinbach /Usenet wrote:
    >
    >> * Ruben Safir, on 12.04.2011 01:39:
    >>> as I understand things, the operator<<() needs to be external as to the
    >>> class and then declared as a friend of the class. I'm not sure if I
    >>> understand why that is the case.

    >>

    >
    >
    > what?? operator<<() can be a normal class member?


    Yes, operator<< can be a normal class member. That says that the expression

    a << b (*)

    where a is of type A and b is of type B can be interpreted in two ways:

    1) operator<<( a, b )
    2) a.operator<<(b)

    That is, in (*) the operator<< could be a member function of A. Even if B
    has a member function operator<<(), it is not be used to interpret (*).

    Hence, when declaring output operators for the use with ostreams, it does
    not help to make them member functions of the class you want to set up for
    output. However, when deriving from streams, you can make them member
    functions.


    Best,

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Apr 12, 2011
    #4
  5. On 12 avr, 05:25, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * Ruben Safir, on 12.04.2011 04:43:
    >
    > > On Tue, 12 Apr 2011 04:01:25 +0200, Alf P. Steinbach /Usenet wrote:

    >
    > >> * Ruben Safir, on 12.04.2011 03:59:
    > >>> On Tue, 12 Apr 2011 02:29:10 +0200, Alf P. Steinbach /Usenet wrote:

    >
    > >>>> * Ruben Safir, on 12.04.2011 01:39:
    > >>>>> as I understand things, the operator<<() needs to be external as to
    > >>>>> the class and then declared as a friend of the class.  I'm not sure
    > >>>>> if I understand why that is the case.

    >
    > >>> what??  operator<<() can be a normal class member?

    >
    > >> Yes.

    >
    >
    > >http://en.wikibooks.org/wiki/C _Programming/Operators/Operator_Overl...

    >
    > > Of special mention are the shift operators,<<  and>>. These have beenoverloaded in the standard library for interaction with streams. When overloading these operators to work with streams the rules below should be followed:

    >
    > >     1. overload<<  and>>  as friends (so that it can access theprivate variables with the stream be passed in by references
    > >     2. (input/output modifies the stream, and copying is not allowed)
    > >     3. the operator should return a reference to the stream it receives (to allow chaining, cout<<  3<<  4<<  5)

    >
    > Your question seems to be missing.


    Because IMHO your answer was misleading.

    Tes, In the general case, operator<<() - i.e. shift operator - can be
    defined as a member function.

    AFAIS Ruben Safier was asking about operator<<() for stream output. In
    this case, yes, he must defined it as a free function (friend or not).

    --
    Michael
     
    Michael Doubez, Apr 12, 2011
    #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. RA Scheltema
    Replies:
    3
    Views:
    424
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    490
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,283
  4. Amadeus W. M.
    Replies:
    2
    Views:
    416
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    320
Loading...

Share This Page