• Advertisement
Sign in to follow this  

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

This topic is 4469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement