Let's say I have a Class Box
class Box{
...
};
and I need to create boxes based on users input. I tried to make a Blueprint Object
Method 1:
int main(){
Box box; //Blueprint
vector<Box> boxes;
for(int i = 0; i < 5; i++) //Creates 5 boxes
boxes.push_back(box);
return 0;
}
This works. It creates a Vector with 5 Box Objects. But then I tried to do the following so I can set values every-time I create one Object with the constructor
Method 2:
int main(){
vector<Box> boxes;
for(int i = 0; i < 5; i++){ //Creates 5 boxes
//Lets assume we input x and y every time
Box box(x, y);
boxes.push_back(box);
}
return 0;
}
This seems to be working just fine, when use boxes[i].x
it gives me the value of x as it should. Note that x and y are Public and not Private for the simplicity of the example.
I saw this post, where the user patrick explained how to create Objects Dynamically using a lot of classes named Factories of Boxes for example and I did not understand you have to complicate it so much instead of using one of the two methods I tried in this post and worked for me.
My questions are:
- Are my methods the right way to make objects based on the users input
- If yes, which one of my methods is the best out of the two
- What is the difference between my way and patrick's from the other post
The original program I am making has a class named AI and I want to make more Objects of this class based on how many AI the user wants to play against.