I want to use a stringstream internally, but to conform to an api I want to return a char*.
Only this does not work:
[CODE=cpp]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class A
{
ostringstream my_ostringstrea m;
public:
void generate()
{
my_ostringstrea m << "do something";
}
const char* get()
{
return my_ostringstrea m.str().c_str() ;
}
};
void main()
{
A a;
a.generate();
cout << "data: " << a.get() << "\n";
}
[/CODE]
using ms visual studio 2005 I get a "debug assertion failed! [...] _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)" at runtime.
Ok, so maybe my_ostringstrea m.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?
Only this does not work:
[CODE=cpp]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class A
{
ostringstream my_ostringstrea m;
public:
void generate()
{
my_ostringstrea m << "do something";
}
const char* get()
{
return my_ostringstrea m.str().c_str() ;
}
};
void main()
{
A a;
a.generate();
cout << "data: " << a.get() << "\n";
}
[/CODE]
using ms visual studio 2005 I get a "debug assertion failed! [...] _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)" at runtime.
Ok, so maybe my_ostringstrea m.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?
Comment