Possible Duplicate:
Why does writing to temporary stream fail?
I'm trying to derive a class from ostrginstream
for some custom logging code. Oddly, the first thing written to the stream always gets mangled. Here's a minimal, self-contained demonstration program:
#include <iostream>
#include <sstream>
using namespace std;
class Test : public ostringstream
{
public:
Test() { ; }
~Test() { cerr << "!" << str() << "!" << std::endl; }
};
int main(void)
{
Test() << "Hello" << ", " << "World";
}
Expected output is: !Hello, world!
Actual output is:
I'm assuming something is wrong with the way I'm creating and using the temporary object.
If it can't easily be fixed, is there another way to get the same semantics? What I'm looking for is the ability to easily create an object, shift into it, and then do something with the complete message in the destructor. (The use case is custom logging code. The destructor will timestamp, check severity, and so on.)