help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fstream::rdbuf() call vs. ios::rdbuf()


From: Ulrich Eckhardt
Subject: Re: fstream::rdbuf() call vs. ios::rdbuf()
Date: Sat, 19 Nov 2005 13:15:56 +0100
User-agent: KNode/0.9.2

Cyril Zorin wrote:
> When I do something like this
> 
> my_fstream.ios::rdbuf(some_ptr);
> 
> to change the streambuf that the fstream uses, this actually changes
> basic_ios' streambuf and does not alter fstream's streambuf.
> 
> Is this the intended behaviour? libstdc++ has a comment that due to a
> design error that LWG doesn't want to correct, fstream::rdbuf() hides
> both overloads of ios::rdbuf() -- but is this hiding just a "synthetic"
> thing, or does fstream completely ignore basic_ios' streambuf?

No, fstream does not ignore the base's streambuffer. The general thing is
that fstream basically has this structure (the same applies to
stringstreams, too, btw):

class fstream: public iostream
{
   filebuf m_fb;
public:
   fstream(): iostream(&m_fb) {}
   filebuf* rdbuf()
   {  return &m_fb; }  
};

(Note: it is a bit more complicated due to initialisation order, but
basically that is it. )

This means that all it does is bundle a filebuf (as member) with an
iostream (as baseclass) and provide an overload for rdbuf() that returns a
filebuf instead of a streambuf. There are no other memberfunctions that
are overridden or any other real behaviour that fstream adds apart from
open/is_open/close which are just forwarded to the filebuf.

The fact that it hides the baseclass' rdbuf() is the unfortunate thing
there, but in practice it doesn't matter much - if you want to redirect
the stream, use an iostream from the beginning, if you only want a target
to redirect to use a filebuf or just read the fstream's filebuf.

Uli

-- 
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]