Reputation: 9504
The basic_iostream
class template includes two overloads for rdbuf()
- one is a const
member function which returns a pointer to the internal streambuf in use, the other takes a single streambuf*
argument, and sets the internal pointer.
However, std::stringstream
overloads the rdbuf()
function as follows:
stringbuf_type* rdbuf() const;
Since only the "getter" version is overloaded, the "setter" version which takes an streambuf*
argument is hidden (due to member function hiding), and thus inaccessible via a stringstream
instance.
Is there some reason this was done? Is it possible to change the internal streambuf
object used by a stringstream? It seems this might be desirable in some situations, say - for example, you want to use a stringstream object that converts all input to lowercase. A custom stringbuf class deriving from std::stringbuf
would be ideal in that case. However, without the rdbuf(streambuf*)
function, there's no way to change the internal streambuf object used by stringstream
.
So, why doesn't std::stringstream
provide a way to set the internal stringbuf object used?
note: to avoid confusion, I'm not talking about setting the internal memory buffer of a stringstream, rather I'm talking specifically about setting the internal std::basic_stringbuf
object used.
Upvotes: 1
Views: 1351
Reputation: 137860
Is there some reason this was done?
Since accessing the streambuf
is more common than changing it, that case was made more convenient.
The special functions added by stringstream
won't work unless the buffer is a streambuf
, so it doesn't really make sense to treat it as a stringstream
with a different buffer. You can use the entire base iostream
class interface, though.
Is it possible to change the internal
streambuf
object used by a stringstream?
Sure, just qualify the member access: s.iostream::rdbuf( newbuf )
. You should similarly qualify any subsequent call to the rdbuf()
accessor if the buffer isn't a std::stringbuf
, to avoid an illegal cast to that class.
Or, just create an iostream &
reference and forget the original stringstream
, except for the purpose of destroying it.
Upvotes: 2