Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7541

Re: iostream manipulator construction and design

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: iostream manipulator construction and design
Date 2011-07-06 19:13 +0200
Organization A noiseless patient Spider
Message-ID <iv24u2$pbf$1@dont-email.me> (permalink)
References <iv0iqd$alg$1@reader1.panix.com> <iv1tcj$mbj$2@reader1.panix.com>

Show all headers | View raw


* 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 <url: http://alfps.wordpress.com>

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 02:55 +0000
  Re: iostream manipulator construction and design Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 01:00 -0700
  Re: iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 15:01 +0000
    Re: iostream manipulator construction and design "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-06 19:13 +0200
      Re: iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 18:00 +0000
      Re: iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-07 02:51 +0000
        Re: iostream manipulator construction and design "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-07 11:13 +0200

csiph-web