/home/boudevil/Projects/Generic/Personnel/LOANI-testing/LOANI-0.6/LOANI-repository/ceylan/Ceylan/trunk/src/code/generic/CeylanStringUtils.h
Converts a constant string into a 'char *', not a 'const char *'. This method still works but has been deprecated in favor of a better solution, the use the
const_cast
C++ keyword, see example below.
This is especially useful for broken old C libraries which have 'char *' arguments where they should ask only for 'const char *': STL string cannot be used directly since the std::string c_str() method returns a 'const char *'.
- Note:
- Memory gets allocated by this function, one should therefore free, with 'delete []', the returned char * when finished with it: the caller is responsible for the returned char * deallocation, otherwise memory is leaked.
- Returns:
- the requested 'char *'. If ever there was enough memory, the application is stopped in emergency.
void aStupidFunction( char * name ) ;
const string aString = "Ceylan rocks!" ;
char * convertedString = Ceylan::getNonConstCharFrom( aString ) ;
aStupidFunction( convertedString ) ;
delete [] convertedString ;
...
or:
...
aStupidFunction( const_cast<char *>( aString.c_str() ) ) ;
...