newbie - boost::shared_ptr of type stringstream using << operator?

Hi, Is it possible to use shared_ptr to manage stringstreams, and still use the << operator on the stringstream? example: //I would like to print 'hello world!' in both cases #include <iostream> boost::shared_ptr< std::stringstream > sharedStream(new std::stringstream("hello world")); (*sharedStream) << "!"; std::cerr << (*sharedStream).str() << std::endl; std::stringstream tr; tr << "hello world"; tr << "!"; std::cerr << tr.str() << std::endl; output: !elloworld hello world! Many thanks, Ed Johnson

Sebastian Redl wrote:
Ed Johnson wrote:
Hi,
Is it possible to use shared_ptr to manage stringstreams, and still use the << operator on the stringstream?
You're doing it. The only problem with this code is a misunderstanding of how the stringstream constructor works.
Sebastian Redl
Thanks, I see where I was not using the correct constructor before. However, even when I use the proper one, I get the same results. I cannot get the '<<' operator to append to the original string that I pass using the stringstream constructor. It still begins at the first character and begins overwriting the string. //constructor syntax explicit stringstream ( openmode mode = in | out ); explicit stringstream ( const string & str, openmode mode = in | out ); //updated code boost::shared_ptr< std::stringstream > sharedStream(new std::stringstream("hello world", std::stringstream::out)); Is there something else I'm missing? Thanks, Ed
participants (2)
-
Ed Johnson
-
Sebastian Redl