How to use wxString::Prepend? 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
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

How to use wxString::Prepend?

Post by softport »

Hello, I've been going round in circles (also cussing a bit) and just
can't figure out how this function works.

I just have a number, and would like to prepend a decimal point:

Code: Select all

    wxString returnString = wxT("123");

    returnString = wxString::Prepend(returnString, wxT("."));
There's no 'coma' in the help file's description of the function, but I have
tried it without, tried it without 'wxT()'.. tried a lot of things.. nothing.

thanks!
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to use wxString::Prepend?

Post by PB »

wxString& wxString::Prepend(const wxString &str)
Prepends str to this string, returning a reference to this string.

Code: Select all

wxString returnString = wxT("123");
returnString.Prepend(wxT("."));
// returnString = ".123"
But there are better ways to format a number. You should also consider using locale specific decimal separator instead of a hard-coded one.
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: How to use wxString::Prepend?

Post by softport »

Thanks pb!

I guess I have a more basic question: how to interpret the help file
function descriptions. I just don't understand how these two are
equivalent:

wxString& wxString::Prepend(const wxString &str) <=> my_wxTstring.Prepend(wxT("."))

I am very new to wxWidgets.
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to use wxString::Prepend?

Post by PB »

softport wrote:I just don't understand how these two are
equivalent:
wxString& wxString::Prepend(const wxString &str) <=> my_wxTstring.Prepend(wxT("."))
I'm sorry, I'm not sure I understand the question.
wxString = class, I assume my_wxTstring is an instance of wxString you declared like

Code: Select all

wxString my_wxTstring;
wxT(".") gets converted to wxString, so my_wxTstring::Prepend() is called with a temporary wxString instance created from wxT("."). wxT is used to wrap string literals, so they're narrow character strings in ANSI build and wide character ones in Unicode builds. But all this is more ore less basic C++ , not really related to wxWidgets?
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: How to use wxString::Prepend?

Post by softport »

Thanks pb, and yes, I am also new to C++. If I could just continue with this a little
further, I would really like to get it out of the way, if that's ok.

I did mean that wxString had already been declared:

Code: Select all

wxString my_wxTstring;
The confusing part is the dot between my_wxTstring and Prepend(wxT(".")) .

Is this how I should interpret it:

Code: Select all

wxString my_wxString;          // Here my_wxString is not a simple string, 
                               // but an instance of the class wxString. 
                               // Class wxString has a member function Prepend().

my_wxString.Prepend(wxT("."))  // Here I am calling the Prepend() member function
                               // of my new wxString class, and passing it another 
                               // (temporary) instance of a wxString class created 
                               // form wxT(".").
                               // Prepend() modifies my_wxString class, then destroys
                               // the temporary instance of wxString class it created
                               // from wxt(".")
From the help file:

Code: Select all

wxString::Prepend
wxString& Prepend(const wxString& str)

Prepends str to this string, returning a reference to this string.
What confused me was "returning a reference to this string". From regular C, I thought
the first wxString& was the address returned by the function wxString::Prepend(const wxString& str).
Why does it say that it returns a reference to a string, when it's actually modifying my_wxString,
an instance of the wxString class? I don't see how the two are equivalent.

Thank you for your patience :)
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to use wxString::Prepend?

Post by PB »

softport wrote:I did mean that wxString had already been declared:

Code: Select all

wxString my_wxTstring;
The confusing part is the dot between my_wxTstring and Prepend(wxT(".")) .
What dot? The dot (which serves as a decimal point in your case, right?) is there because you want to prepend it to the string.The line above declares my_wxTstring, an instance of wxString class. Not sure why you have "wxT" as a part of the variable name there, Do you really understand what wxT() macro does (see my previous post)?
softport wrote:Is this how I should interpret it:

Code: Select all

wxString my_wxString;          // Here my_wxString is not a simple string, 
                               // but an instance of the class wxString. 
                               // Class wxString has a member function Prepend().

my_wxString.Prepend(wxT("."))  // Here I am calling the Prepend() member function
                               // of my new wxString class, and passing it another 
                               // (temporary) instance of a wxString class created 
                               // form wxT(".").
                               // Prepend() modifies my_wxString class, then destroys
                               // the temporary instance of wxString class it created
                               // from wxt(".")
Again, my_wxString is not a class, but an instance (variable of type) of wxString, but aside from that, you're correct.
softport wrote:From the help file:

Code: Select all

wxString::Prepend
wxString& Prepend(const wxString& str)

Prepends str to this string, returning a reference to this string.
What confused me was "returning a reference to this string". From regular C, I thought
the first wxString& was the address returned by the function wxString::Prepend(const wxString& str).
Why does it say that it returns a reference to a string, when it's actually modifying my_wxString,
an instance of the wxString class? I don't see how the two are equivalent.
wxString::Prepend() does exactly what the documentation says. Modifies the content of the string and then returns a reference to itself, which you can ignore if you don't need it. By "this string" it is meant the instance of wxString you called Prepend() for.
softport wrote:I am also new to C++.
I can only recommend you to hit a good C++ primer, it will make your life much easier and in the end it should also spare you a lot of time and frustration. ;)
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: How to use wxString::Prepend?

Post by softport »

Many thanks pb for clearing up most of the fog.
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
Post Reply