c_str like function implementation
Hi...
Does anyone know how to implement a function that returns a const char* for use in this situation:
Code:
class foo
{
protected:
int i_size;
char* c_data; // not necesarily null terminated
...
public:
const char* c_str()
{
// - how to implement ?
// - should add a null terminator to c_data
// - not neccesary: possybitlity of aditional manipulation of i_data
// ( concat with another string )
// - only the returned string should contain the changes - i_data should
// not be changed in the process
}
...
};
void bar( const char* c )
{
// operations on the null terminated c string
}
in main:
foo test( "abc" );
bar( test.c_str() ); // this part uses c_str() function to convert data to the bar function
// input type ( const char* )
The part that is causing problems is that I don't want to use malloc in c_str() since that would produce a memory leak in such a case.
Any ideas anyone?
Thanks, Domen.