Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: templated operator()<<

Reply
Thread Tools

Re: templated operator()<<

 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-12-2011
* 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>
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-12-2011
* 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>
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-12-2011
* 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>
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-12-2011
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
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      04-12-2011
On 12 avr, 05:25, "Alf P. Steinbach /Usenet" <alf.p.steinbach
(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling templated member of templated object david@sunlightd.com C++ 1 06-22-2007 05:13 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments