Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #3442

Re: templated operator()<<

From Ruben Safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject Re: templated operator()<<
Date 2011-04-12 02:37 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <io0dtf$qsv$2@reader1.panix.com> (permalink)
References <io03f9$qsv$1@reader1.panix.com> <io06co$fgm$1@dont-email.me> <io0bl5$9hr$1@reader1.panix.com> <io0bpn$dph$2@dont-email.me>

Show all headers | View raw


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://www.google.com/url?sa=t&source=web&cd=6&ved=0CD4QFjAF&url=http%3A%2F%2Fwww.cs.duke.edu%2Fcsed%2Ftapestry%2Fhowtoe.pdf&ei=5LejTczhDsLVgQfbrLmeCg&usg=AFQjCNHOXIvtfFFJPEpToqsEn6P_sXqPuw&sig2=fIiugNyxWwZH49lWIYorug

and other locations (on in ym text books)

We’ll look first at overloading the insertion operator, operator <<, for stream output.
Here it’s absolutely not possible to make the operator a member function of the class for
which output is being defined. The statement
    cout << x;
could be interpreted by the compiler as cout.operator <<(x) , where the inser-
tion operator is a member function of the ostream class of which cout is an instance.
The insertion operator could also be a free function with two parameters, much like
operator + is as discussed above. Since programmers don’t typically have access to
redefining the standard I/O classes, the I/O operators are typically implemented as free
functions. The header for the insertion operator for BigInt is shown below.
    ostream& operator <<(ostream & out, const BigInt & big)
    // postcondition: big inserted onto stream out
The return type must be a reference type because the stream on which the object is
inserted is returned for subsequent insertion operations. This is what allows insertions
to be chained together:
    BigInt b = factorial(val);
    string s = " factorial = ";
    cout << s << b;
The last statement could be written more cumbersomely as follows since operator <<
is overloaded as a free function for both string and BigInt objects.
       operator << (operator << (cout,s), b);
However, it’s essential that operator << be an operator and not a function since the
order in which arguments are evaluated in C++ is not defined. In the statement x =
min(sqrt(x),sqrt(y)), compilers are not required to evaluate sqrt(x) before
evaluating sqrt(y) (this is a C legacy, it’s too bad that the order in which arguments
are evaluated isn’t prescribed). However, the associativity of operator << is defined,
it’s left associative, which means that
    cout << x << y << z;
requires that x be inserted before y, and that y be inserted before z.
     Now that we know the prototype for the overloaded operator <<, how do we
implement the operator? As with overloaded arithmetic operators there are two choices:
        Make operator << a friend of the class whose output is being overloaded, for
        example, of BigInt in the examples above.
        Create a member function that can be used in implementing operator << as a
        free, nonfriend function.
June 7, 1999 10:10 owltex     Sheet number 31 Page number 754          magenta black
      754             Appendix E How to: Overload Operators
                          We’ll adopt the second approach, since it avoids the coupling entailed by creating
                      friend classes and the solution we’ll use is easily extensible to other, nonstream output,
                      such as on a graphics display.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-11 23:39 +0000
  Re: templated operator()<< LR <lruss@superlink.net> - 2011-04-11 20:07 -0400
  Re: templated operator()<< "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 02:29 +0200
    Re: templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-12 01:59 +0000
      Re: templated operator()<< "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 04:01 +0200
        Re: templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-12 02:37 +0000
        Re: templated operator()<< Ruben Safir <ruben@mrbrklyn.com> - 2011-04-12 02:43 +0000
          Re: templated operator()<< "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 05:25 +0200
            Re: templated operator()<< Michael Doubez <michael.doubez@free.fr> - 2011-04-12 00:17 -0700
      Re: templated operator()<< Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-12 08:23 +0200

csiph-web