views:

174

answers:

2

How can i set a new char array to be the buffer of a fstream's filebuf, there is a function (setbuf) in the filebuf but it is protected. while searching on the web, some sites mention fstream::setbuf but it doesn't seem to exist anymore.

Thanks

A: 

The point of a protected member function is that it is meant to be overridden by your derived subclass. Here is a short section of the libstdc++ manual which discusses exactly that.

Here's an excerpt from <streambuf> which makes the same point in code:

  // [27.5.2.4.2] buffer management and positioning
  /**
   *  @brief  Manipulates the buffer.
   *
   *  Each derived class provides its own appropriate behavior.  See
   *  the next-to-last paragraph of 
   *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
   *  for more on this function.
   *
   *  @note  Base class version does nothing, returns @c this.
  */
  virtual basic_streambuf<char_type,_Traits>* 
  setbuf(char_type*, streamsize)
  { return this; }
msw
i understand the point of protected members, but it wasn't really the aim of the question.Thanks anyway
Kay25
So what was the aim of the question? Whether the function "doesn't seem to exist anymore"? That's false, it's part of the standard ISO/IEC 14882:1998(E) for which I cited chapter and verse.
msw
http://www.open-std.org/jtc1/sc22/open/n2356/lib-iostreams.html#lib.streambuf.virt.buffer
msw
+1  A: 

streambuf is designed to be customized using the template method pattern, where the public methods are not virtual, and subclasses customize the behavior by implementing non-public virtual methods.

In the case at hand, the public method which calls setbuf is named pubsetbuf.

Note however that basic_filebuf's implementation of setbuf is rather loosely specified: The only guarantee is that setbuf(0, 0) makes the stream unbuffered. In other cases, the effect is implementation-defined.

Éric Malenfant