![]() |
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> |
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> |
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> |
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 |
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.