Steven C wrote:
>
> I have a 3rd party libary that takes a char * (not a const char *).
>
> if I have:
>
> string strSome;
> char *the3rdpartylib;
>
> how do I:
> the3rdpartylib = strSome.c_str();
>
> I have done:
> the3rdpartylib = (char *) strSome.c_str();
>
> which gets by the compiler messages but the lint program is complaining about
> never assign a const to a non const.
>
> I tried this:
> the3rdpartylib = static_cast <char *> (strSome.c_str());
>
> which gives compiler error message:
> can't convert from const char * to char *
You want either of 2 things:
1) cast away the const ness
2) create a duplicate of the string as a C-style string
Which strategy you choose, depends largely on what the third
party library does with that C-style string:
* If it leaves it alone and just does read accesses to that string
you can take approach 1)
* If the library modifies the passed string, you have to resort
to approach 2)
for 1) the3rdpartylib = const_cast< char* > ( strSome.c_str() );
for 2) size_t len = strSome.length();
char* tmp = new char [ len + 1 ];
strcpy( tmp, strSome.c_str() );
... call the function
strSome = tmp;
delete [] tmp;
--
Karl Heinz Buchegger