0

I need create 220 object. The normal creation is like the code below; Is there any way more easy to create them? maybe a for Loop...

const int ID_box1 = 1; 

box1 = new Boxes(ID_box1, position(10,10); 

box1->Append("option 1");
box1->Append("option 2"); etc..

// each box have 80 options:

mrsoliver
  • 91
  • 2
  • 8
  • 4
    Yeah, a loop sounds good. a `std::vector` sounds even better, frankly. Give it a try. (if nothing else, it may solve the missing `)` in your posted code because you don't need to do that anymore). – WhozCraig Jun 03 '19 at 05:33
  • To store the objects, use an array or an `std::vector`. To add the options, you might want to construct the name of the option using `std::to_string`. There are plenty of resources out there such as [good books](https://stackoverflow.com/q/388242/9254539) to teach you how to use arrays and loops. We don't teach people basic stuff like this from scratch here. – eesiraed Jun 03 '19 at 06:26

1 Answers1

1

If you are sure about the number of objects to be created, then, you may use array as follows:

#include <array>

std::array<Box, 220> boxes; // assumes default constructor is available for Box class. 

std::array gives performance benefit, it's usage is similar to normal array of objects plus it acts as a container so if needed, the applicable standard library algorithm functions can be used.

If more flexibility, functionality is needed, then, std::vector is good choice.

Rushikesh
  • 163
  • 8