For a project, I’d like to use stringstream to carry on data. To achieve this goal, I have to pass some stringstream as parameter to some function, but when I output the stringstreams, I see something like an address.
The code :
#include <iostream>
#include <sstream>
void doStuff(const std::iostream& msg)
{
std::cerr << msg << std::endl;
}
int main(void)
{
doStuff(std::stringstream("av"));
}
The output is :
0xbff4eb40
Can someone explains why I get an address when passing an rvalue ?
And why can’t I pass a stringstream by value ?
You probably want to access the string on which the stringstream is storing its data:
What is happening in your code is that
iostreamscontain avoid* operatorwhich returns 0 if the stream contains any error or has reached EOF, and another value otherwise. This is usefull for error checking.When you try to write you stream to
std::cerr, the compiler realizes that the stream can be converted to a void* using that operator, and that a void* can be written to a ostream(the operator<< has been defined), and therefore uses it.Note that i changed the method’s signature so that it receives an std::stringstream as an argument, since std::iostream::str is not defined(this method is only available on string streams).