construct wxString with NULL Topic is solved

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.
Post Reply
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

construct wxString with NULL

Post by MoonKid »

I am not sure if I read the wxString code right. But is it possible to construct a empty wxString like that:

Code: Select all

wxString str(NULL);
Of course, it is dirty in this case. But generaly: is it possible?
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

From wxWidgets documentation:
Your code corresponds to

Code: Select all

wxString(const wxChar* psz, size_t nLength = wxSTRING_MAXLEN)
// or
wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN)
Therefore, it must be correct.
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Why not use wxEmptyString ?

wxString name = wxEmptyString;

It assigns a null kind of wxString to your wxString. It is more elegant then using a NULL which you do not know what exactly happens.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
krzysiek_t
Earned a small fee
Earned a small fee
Posts: 23
Joined: Tue Jul 11, 2006 1:36 pm
Location: Poland, Warsaw
Contact:

Post by krzysiek_t »

And - as always - you can create empty string using the default constructor:

Code: Select all

wxString empty_str;
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Yep, but wxEmptyString is usually very handy for default arguments and returns by reference. Like;

Code: Select all

myfunction(int arg1, int arg2, wxString &name = wxEmptyString);
Where the last can be omitted.

Also returning after an error,

Code: Select all

{
  wxCHECK2(somecond == false, wxEmptyString);
  return someotherstring;
}
If wxCHECK2 fails, in debug this leaves an assertion. In release code it expands to:

Code: Select all

if(somecond == false)
  return wxEmptyString;
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

And there are even internal mechanics that avoid refcounting when using wxEmptyString....
[Mostly retired moderator, still check in to clean up some stuff]
framepointer
Super wx Problem Solver
Super wx Problem Solver
Posts: 264
Joined: Mon Aug 07, 2006 3:25 pm
Location: Baia Mare, Romania
Contact:

Post by framepointer »

And make sure that you always decalare function arguments as

Code: Select all

const wcString &
when using wxEmptyString.

Actually it better to init strings like
wxString str(_T("")) because using the default contructor gives valgring errors : invalid read of size 1 (under wx 2.6.3).
Software is like sex,
It's better when it's free.
~Linus Torvalds
Post Reply