Sign in to follow this  
sipickles

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

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this