This, apparently, is a consequence of how you create the object. I am still studying (and you can get a better answer at the same time), but explicit object creation works fine:
#include <iostream> #include <sstream> class StreamWriter { public: StreamWriter() {} ~StreamWriter() { std::cout << m_stringstream.str() << std::endl; } std::stringstream m_stringstream; }; int main (void) { StreamWriter *sw = new StreamWriter(); sw->m_stringstream << "Hello " << "my " << "name is Pris "; delete sw; return 0; }
As for the instance on the stack:
int main (void) { StreamWriter sw; sw.m_stringstream << "Hello " << "my " << "name is Pris "; return 0; }
Both of them print out what you expect, but the following simplification of your code:
int main (void) { StreamWriter().m_stringstream << "Hello " << "my " << "name is Pris "; return 0; }
If you have just decided a solution, you can probably:
#define ORSDEBUG StreamWriter sw; sw.m_stringstream
and ensuring that you protect this area so that it does not try to create more than one sw , and that it is destroyed at the right time, as well as your initial attempt:
{ ORSDEBUG << "Hello " << "my " << "name is Pris"; }
source share