Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: templated operator()<< (http://www.velocityreviews.com/forums/t746666-re-templated-operator.html)

Alf P. Steinbach /Usenet 04-12-2011 12:29 AM

Re: templated operator()<<
 
* 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 04-12-2011 02:01 AM

Re: templated operator()<<
 
* 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 04-12-2011 03:25 AM

Re: templated operator()<<
 
* 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++_Pro...mber_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>

Kai-Uwe Bux 04-12-2011 06:23 AM

Re: templated operator()<<
 
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

Michael Doubez 04-12-2011 07:17 AM

Re: templated operator()<<
 
On 12 avr, 05:25, "Alf P. Steinbach /Usenet" <alf.p.steinbach
+use...@gmail.com> 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++_Pro...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


All times are GMT. The time now is 10:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57