MESSAGE
DATE | 2016-11-26 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Learn] operator<<() overloading details and friend
|
From learn-bounces-at-nylxs.com Sat Nov 26 18:08:10 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 7EE6D161313; Sat, 26 Nov 2016 18:08:10 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id BA8A5160E77 for ; Sat, 26 Nov 2016 18:08:07 -0500 (EST) From: Ruben Safir To: learn-at-nylxs.com X-Mozilla-News-Host: news://news.panix.com Message-ID: <2b5ff360-531d-ad7b-55b6-5d22ea8c9f87-at-mrbrklyn.com> Date: Sat, 26 Nov 2016 18:08:07 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Subject: [Learn] operator<<() overloading details and friend X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
* zacklocx (~zacklocx-at-114.111.167.113) has joined ##c++ * zacklocx has quit (Remote host closed the connection) sorry -- should be `[const] Input` for copy by value mrbrklyn: Did you mean operator<<()? yes operator<<() * Amado has quit (Ping timeout: 245 seconds) * CaptainPi has quit (Quit: Konversation terminated!) mrbrklyn: And did you mean operator<<(std::ostream&, const T &)? * sid_fules (~sid_fules-at-79-75-34-79.dynamic.dsl.as9105.com) has joined ##c++ i.e. the "streaming" operator, rather than the bitshift operator? std::ostream& operator << ( std::ostream &output, state const & p ) yes * RajRajRaj has quit (Quit: Connection closed for inactivity) * vuoto has quit (Remote host closed the connection) it doesn't need to be a friend unless it needs special access You may want to print out some internal state. To do so, you may require access to private or protected properties. * vuoto (~vuoto-at-80-71-135-9.u.parknet.dk) has joined ##c++ why not just make it part of the class like other operators? * InfoTest has quit (Quit: InfoTest) * escapade has quit (Quit: Leaving.) One workaround would be to define a public member function void print(std::ostream &), though; then you op<< can just call that (p.print(output); return output; in your example) instead of having to be declared a friend. * smwangi has quit (Ping timeout: 252 seconds) mrbrklyn: Because if you make an op<< a member function of a class, its left hand side operator will be an instance of the class it is defined in. mrbrklyn: you can't make it a member because that would mean the left side is of the class type So you'd need to define it inside of std::ostream - which obviously is a bad idea. its left side must be ostream& * smoon has quit (Ping timeout: 245 seconds) * IPXSam has quit (Quit: Leaving) * Rutix (~rutix-at-4daece29.ftth.telfortglasvezel.nl) has joined ##c++ * Rutix has quit (Changing host) * Rutix (~rutix-at-april-fools/2013/runnerup/Rutix) has joined ##c++ * speidy` (~Idan-at-bzq-79-177-52-145.red.bezeqint.net) has joined ##c++ because it can be used as an l value? * melita (~melita-at-2601:645:4000:ccd0:1092:2f75:1b74:5ab0) has joined ##c++ * f10 (~flo-at-2001:470:52af:1:400c:1a92:409a:a3e4) has joined ##c++ I only seem to ever sort of understand this, and never fully understand it it has defied me for years If you define any operator as a member function, the type it is defined inside is the left-hand side argument Just by the definition of member functions However, arbitrary user-defined types want to overload the stream operator to use ostream They can't all be inside of the ostream class because ostream doesn't know about those types * FreeBirdLjj (~freebirdl-at-99-1-240-26.lightspeed.rswlga.sbcglobal.net) has joined ##c++ mrbrklyn: You can define them as a friend within the class, though. Or as just a free function * everywhen (~everywhen-at-74.83.79.229) has joined ##c++ * Kenran has quit (Quit: leaving) struct my_type { friend std::ostream &operator<<(std::ostream &os, const my_type &obj) { return os << obj.value; } private: int value; }; ^ is that a free function? It is. * CEnnis91 has quit (Quit: Connection closed for inactivity) It is shorthand for: struct my_type { friend std::ostream &operator<<(std::ostream &os, const my_type &obj); private: int value; }; std::ostream &operator<<(std::ostream &os, const my_type &obj) { return os << obj.value; } Plus I'd assume it might also imply op<< being inline, just like defining member functions within the class definition. Not 100% sure about that, though. friend op<< is a code smell, because if op<< can't be implemented with the public interface, it means that you may have an object x with a property p where the only way to read x's p is to first print x and then parse what was printed which is silly, because in such a case there should normally be a way to just get x's p without going through serialization
-- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013 _______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
|
|