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:

2
  • 4
    Yeah, a loop sounds good. a std::vector<Boxes> 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). Commented Jun 3, 2019 at 5: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 to teach you how to use arrays and loops. We don't teach people basic stuff like this from scratch here. Commented Jun 3, 2019 at 6:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.