Page 1 of 1

How to convert wxString type to char* type ?

Posted: Wed Feb 04, 2015 11:37 am
by dqf88
How to convert wxString type to char* type ? I dealt with it like this,but don't work,why?

Code: Select all

wxString str="I' m a string!";
char* s=str.mb_str();

Re: How to convert wxString type to char* type ?

Posted: Wed Feb 04, 2015 12:31 pm
by DavidHart
Hi,

(I moved this thread, like your last one, from 'General Development' to here as they are both standard C++ issues.' General Development' is for things that don't fit comfortably elsewhere.)

str.mb_str() returns a temporary pointer, which immediately loses scope. See https://wiki.wxwidgets.org/Converting_e ... to_char.2A for more information.

Regards,

David

Re: How to convert wxString type to char* type ?

Posted: Thu Feb 05, 2015 12:42 am
by guzzi_jones
What exactly is a temporary pointer? Is there a link to a tutorial to explain that? * edit* it is an object created on the stack. so it is not created with new and is automatically deleted when it goes out of scope . since char * is also a pointer it is setting it's address to the address returned by mb_str(). Then the memory at that address is deleted.

I am new, but here is what i did on a project of mine:

Code: Select all

string ltype_desc=wx_ltype.mb_str(); //this copies the char * into a string object.  the equals has been overloaded to do this.
char * cString=ltype_desc.c_str();// *edit got rid of redundant stringstream
I suppose you could write a class for this to avoid duplicate code.

Re: How to convert wxString type to char* type ?

Posted: Thu Feb 05, 2015 6:56 am
by PB
Guzzi_jones I am afraid you are not correct. Temporary object is not (at least in this context) a programmer defined variable, these are unnamed objects created by the compiler and valid only inside the expression they were used in. In the case of wxString::c_str(), the temporary object is of wxCStrData type.

See e.g. here for more:
http://stackoverflow.com/questions/2506 ... -arguments
http://en.cppreference.com/w/cpp/language/lifetime

That also means that the code you posted is wrong, as it has the exact mistake you are not supposed to make, see here for wxString specific explanation:
http://docs.wxwidgets.org/trunk/overvie ... supportout

Re: How to convert wxString type to char* type ?

Posted: Thu Feb 05, 2015 2:14 pm
by guzzi_jones
thanks PB.
One clarification, though. I didn't use wxstring.c_str(). I first created a str::string object and then used string.c_str().

I am still learning c++ , (obviously), and really appreciate your feedback.

Re: How to convert wxString type to char* type ?

Posted: Thu Feb 05, 2015 7:11 pm
by PB
guzzi_jones wrote:One clarification, though. I didn't use wxstring.c_str(). I first created a str::string object and then used string.c_str().
Ah, I'm sorry, I missed that. On the other hand (and I may be wrong again), std::string is not most suitable for storing multibyte (e.g. UTF-8) strings, as many of its operations assume one character = one byte. There is also wchar_t based std::wstring (and few more string types in C++11), which may be more suited for handling Unicode characters.