If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
As far as I know, GetValue returns a string object, which invokes the mb_str method, which "Returns the multibyte (C string) representation of the string using conv's wxMBConv::cWC2MB method and returns wxCharBuffer. " according to the manual.
Do I understand the code correctly? and what does mb_str return in this case?
P.S
I even deleted mb_str() and the program still compiled and is running fine.
GetValue() returns a wxString, which is not [necessarily] a std::string, so some mechanism might be needed for conversion, and using mb_str() is one of them.
mb_str() returns wxCharBuffer, which is convertible to char*.
If conversion works for you without the call to mb_str(), you might be using wxWidgets libs compiled with wxUSE_STL=1.
Thank you for the reply. But
1. if GetValue doesn't return a string, how could we call mb_str? I think function chaining is only possible with c++ objects. I am saying this because I don't yet know much about c++ as I've only finished reading c++ primer plus.
2. I haven't look at the source code for GetValue, but do you mean there are two versions of GetValue, one that uses the standard c++ string and one that is designed for wxwidgets only?
P.S
as for the wxUSE_STL=1, I use Visual Studio Community 2019 and built wxWidgets from source. I just clicked on the sln file and that's all.
wxString wxTextCtrl::GetValue() const
{
// range 0..-1 is special for GetRange() and means to retrieve all text
return GetRange(0, -1);
}
wxString wxTextCtrl::GetRange(long from, long to) const
{
wxString str;
if ( from >= to && to != -1 )
{
// nothing to retrieve
return str;
}
str is local to GetRange. How is it possible that it should contain the numerical string I entered previously?
newname wrote: ↑Thu Aug 13, 2020 12:32 am
1. if GetValue doesn't return a string, how could we call mb_str? I think function chaining is only possible with c++ objects. I am saying this because I don't yet know much about c++ as I've only finished reading c++ primer plus.
GetValue() always returns a wxString.
newname wrote: ↑Thu Aug 13, 2020 12:32 am2. I haven't look at the source code for GetValue, but do you mean there are two versions of GetValue, one that uses the standard c++ string and one that is designed for wxwidgets only?
Not exactly. But if wxUSE_STL=1 (by default it's 0), then implicit conversions from wxString to std::string exist. This is not enabled by default, see explanation below.
std::string is a "stupid" byte container. wxString is a little more complex than that and you should treat is as a "black box" that stores a Unicode(!) string. Normally you should not care about in which format wxString stores the string internally.
If you want to convert a Unicode wxString to std::string, some kind of conversion needs to happen, because not all Unicode characters can by represented with 8 bits. That's why the original code you posted contains the mb_str() call. Without parameters it's called as mb_str(wxConvLocal). Which means that it tries to encode the Unicode string using the local encoding. This is somewhat "dangerous" as it could fail and return an empty string, e.g. if the string contained Japanese characters.
str is local to GetRange. How is it possible that it should contain the numerical string I entered previously?
You only posted the code part that checks if the passed range is valid, and if not, it just returns an empty string. Further down, "str" is filled with a part of the string.