Reputation: 2563
How can I write:
buttons[0] = imageButton;
buttons[1] = imageButton1;
etc more efficiently. I have over 1000 buttons. I was hoping there is a better way to write this.
I tried the following and was forced to insert an & by XCode. This code crashed my program:
for (int i = 0; i < numberOfButtons; i++)
{
buttons[i] = &imageButton[i];
}
Thanks
Upvotes: 0
Views: 676
Reputation: 76184
It sounds like you're declaring a thousand local variables named imageButton1
through imageButton1000
and now you want to put them in an array.
Button imageButton1("Save");
Button imageButton2("Load");
//...etc...
Button imageButton1000("Reticulate Splines");
//now that we've exhaustively created all the buttons, exhaustively put them into the array
buttons[1] = &imageButton1;
buttons[2] = &imageButton2;
//...etc...
buttons[1000] = &imageButton1000;
In my experience, putting a number in a variable name is a very strong signal that the data shouldn't be declared individually, and instead should be part of a collection of similar objects.
buttons[0] = new Button("Save");
buttons[1] = new Button("Load");
//...etc...
buttons[999] = new Button("Reticulate Splines");
You still have to spend a thousand lines filling the array, but this is unavoidable if you have a thousand completely unique objects. But at least this way is not two thousand lines long.
Upvotes: 0
Reputation: 104698
if you have a vector of pointers and want to fill it quickly, you can just use this:
std::vector<t_button*> buttons(1000, 0); // << is that what you wanted?
for (int i = 0; i < numberOfButtons; i++) {
buttons[i] = &imageButton[i];
}
of course, you'd need to ensure what you add to the vector outlives the vector itself because this is an array of pointers to buttons, not values.
if you have just a ton of free variables with unique addresses and unique names which have monotonically increasing suffixes, you'll likely be much happier in the long run if your store those values themselves in a vector:
std::vector<t_button> buttons(1000, 0); // << holds 1000 buttons by value
overall, the question is hard to answer -- it's worded like a performance question, but there are other semantic issues which need to be addressed first, and a lot of detail is missing.
Upvotes: 1
Reputation: 9
I would suggest using string connecting macro if you want to add imageButton1 to imageButton1000
#define imagebutton(X) imageButton##X
and can call this function from your loop. If you have imagebuttons as array you can do memcpy as well
memcpy(button, imagebutton, noOfButtons)
Upvotes: 0
Reputation: 1552
You can use a vector to hold the address of the buttons
std::vector< ImageButton* > Buttons;
Buttons.push_back( &MyButton );
/* then */
ImageButton* button = Buttons.at(i); // int i, is the index
button->DoSomething();
for example you could
std::vector< ImageButton* > Buttons;
for(int i=0;i<100;i++)
{
ImageButton* button = new ImageButton;
Buttons.push_back( button );
}
for(int i=0;i<100;i++)
{
ImageButton* button = Buttons.at(i);
button->DoSomething();
}
If you're planning to use a fixed size for the container you might as well create a Memory Pool. Which will save you from heap fragmentation and nasty crashes.
Anyways I HIGHLY suggest using a Memory Pool as an alternative of using 'new' alot. 1 new is better than 1000. It could even be 2000% faster.
Upvotes: 0
Reputation: 2813
If you are using new C++ remember about emplace_back and move if possible. I guess button is a pointer? because you "&". Copying pointer and whole object are two different things. You should add a definition of button. Use .reserve to avoid unnecessary copying objects ( std::containers makes copies of objects by default).
Also remember that there in example ptr_vector in boost which will help you to stay clear.
http://www.boost.org/doc/libs/1_51_0/libs/ptr_container/doc/ptr_vector.html
Upvotes: 1