wxList sequential Append() 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
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

wxList sequential Append()

Post by papillon68 »

I have an odd issue on a very simple code, basically appending data to a list will result in storing always the last value. Here is the code:

Code: Select all

WX_DECLARE_LIST(int, colorTableList);
WX_DEFINE_LIST(colorTableList);
colorTableList myColorTableList;
	for (int i = 0; i < 100; i++)
	{
		myColorTableList.Append(&i);
	}
	
	// check data
	for (int k = 0; k < myColorTableList.GetCount(); k++)
	{
		int r = *myColorTableList[k];
		MyApp::showMessage(wxString::Format(wxT("color: %i"), r), "l");	
	}
The data stored and printed it's for always 99 for all the list nodes, and I can't understand why. Thanks for any help.
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxList sequential Append()

Post by doublemax »

The list stores pointers any you're passing the same pointer (&i) for every element.

Use STL containers or the wx wrappers: https://docs.wxwidgets.org/trunk/group_ ... iners.html
Use the source, Luke!
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: wxList sequential Append()

Post by papillon68 »

Thank you doublemax, actually it was very simple to fix it, my bad. Just posting the proper "fix" in case some newbie like me stumbles upon it:

Code: Select all

int *randomColor = new int(i);
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxList sequential Append()

Post by doublemax »

Yes, that is one solution. But allocating memory with new to store a single integer is certainly not the best one :)

I would be better to use a container that can store integers directly.
Use the source, Luke!
User avatar
papillon68
Earned some good credits
Earned some good credits
Posts: 118
Joined: Tue Nov 06, 2007 11:19 pm

Re: wxList sequential Append()

Post by papillon68 »

Ok, so you mean like a std::vector or a wxArray?
Windows 10, MS VC++ 2019 (vc142), WxWidgets 3.14
Designed with WxWidgets: https://www.facebook.com/clorofillaApp
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxList sequential Append()

Post by doublemax »

papillon68 wrote:Ok, so you mean like a std::vector or a wxArray?
Yes
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxList sequential Append()

Post by PB »

I dare to disagree with doublemax and suggest staying away from wxArray. Its documentation starts with
The legacy dynamic array class, existing for compatibility only and NOT to be used in the new code.
I believe that whenever possible, std containers are to be used. But if for some reason you cannot use std::vector, consider using wxVector.
Post Reply