Only this does not work:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- class A
- {
- ostringstream my_ostringstream;
- public:
- void generate()
- {
- my_ostringstream << "do something";
- }
- const char* get()
- {
- return my_ostringstream.str().c_str();
- }
- };
- void main()
- {
- A a;
- a.generate();
- cout << "data: " << a.get() << "\n";
- }
using ms visual studio 2005 I get a "debug assertion failed! [...] _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" at runtime.
Ok, so maybe my_ostringstream.str() creates a local copy and then returns a pointer to a local object? Is that the case? And anyways, can somebody give me a hint on how to resolve that?