cannot convert from 'const wchar_t *' to 'wchar_t *' - boohoo!

Started by
2 comments, last by MaulingMonkey 18 years, 4 months ago
My voyage thru strings and chars and wchar_ts continues.... but with some progress thanks to the persistance of GD.net members. My last(?) problem = cannot convert from 'const wchar_t *' to 'wchar_t *'! I am trying to get a char* derived from a std::wstring, like this: char* tempChar; const wchar_t* tempConstWide = g_IP.c_str(); wchar_t tempWide = const_cast< wchar_t* >tempConstWide; get an error tho: syntax error : identifier 'tempConstWide' If anyone wants to know WHY... ( ia m beginning to wonder ) ... then I am using a DXUT framework (which requires Unicode) with winsocks2, which is MBCS based all the way! My edit boxes outputs LPWCSTR (which I am storing as std::wstrings) and my winsock interface wants *&*%&*^ chars. Wheres the logic in that! Phew! Anyway, please help, before I shrivel up and disappear! Thanks! Simon
Advertisement
wchar_t* tempWide = const_cast< wchar_t* >( tempConstWide );

Quote:Original post by Konfusius
wchar_t* tempWide = const_cast< wchar_t* >( tempConstWide );


Just remember that
wchar_t const *tempConstWide = L"my wide constant";wchar_t* tempWide = const_cast< wchar_t* >( tempConstWide );tempWide[5] = L'z'; // or anthing else that mutates tempWide

is not valid.
If you want a wide character buffer (one to be modified) use this instead:

std::vector< wchar_t > wide_buffer( size_of_buffer );
wchar * example = & wide_buffer[0];

Attempting to modify the contents of the string could:
1) Have no effect, if a roping implementation (buffer is discarded)
2) Modify multiple strings, if a copy on "write" implementation.

std::string/wstring return a const char for a reason, and that's to allow such implementation-specific optimizations as I've described above.

This topic is closed to new replies.

Advertisement