MESSAGE
DATE | 2011-07-07 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] (fwd) Re: iostream manipulator construction and design
|
-- forwarded message -- Path: reader1.panix.com!panix!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!ecngs!feeder2.ecngs.de!78.46.240.70.MISMATCH!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Alf P. Steinbach /Usenet" Newsgroups: comp.lang.c++ Subject: Re: iostream manipulator construction and design Date: Wed, 06 Jul 2011 19:13:22 +0200 Organization: A noiseless patient Spider Lines: 65 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 6 Jul 2011 17:10:27 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="iYQNcHckTm+srThRKCAVyQ"; logging-data="25967"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/iPypTcapKOPG2Y7XybGp8" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051201 Thunderbird/1.5 Mnenhy/0.7.3.0 In-Reply-To: Cancel-Lock: sha1:CgimKg6AjwQeM5YaKeHSbslbeUY= Xref: panix comp.lang.c++:1087338
* Ruben Safir, on 06.07.2011 17:01: > On Wed, 06 Jul 2011 02:55:09 +0000, Ruben Safir wrote: > >> What don't understand a couple of things here, and this might be an >> accumulation of increasing confusion on my part as I've continued >> through this chapter. > > nobody with experience in this?
I'm guessing you didn't quite grok Michael's answer?
OK, it's not that hard. You just have to think about how you would have implemented the design, where the design is that a manipulator expression such as `setWidth(4)` can be "inserted" into the stream. It's a horrible design! :-)
But given that, well, `setWidth(4)` is a function call. As a function call it produces some value. That value is inserted into the stream via <<.
So what value should `setWidth(4)` produce?
It must be a value that somehow remembers the `4`, and it must be a value that isn't just presented as a number or something.
So, you let it be a value of some special class, like
struct MyWidthManip { int w_; MyWidthManip( int w ): w_( w ) {} };
Now you can define the function `setWidth`:
MyWidthManip setWidth( int w ) { return MyWidthManip( w ); }
Goodie, your call `setWidth( 4 )` produces an object of this class.
All you have to do now is to define the effect of inserting that into a stream:
std::ostream& operator<<( std::ostream& stream, MyWidthManip const& m ) { stream.setw( m.w_ ); // I'm just assuming there is a setw. Dunno. return stream; }
And that's it.
For a manipulator without arguments you don't need a supporting class, because the manipulator can just be a function that << operator then calls.
Bjarne's example of a support class was a bit more general than above, namely a support class that supported many different one-int-argument manipulators. That made it a little more complex. With some old C syntax function pointer declaration thrown in -- I don't understand why he would do that in a book!
Cheers & hth.,
- Alf
-- blog at -- end of forwarded message --
|
|