-
Array of class objects
How'd i make an array of class type objects here is my current call to create an instance.....
Code:
cannon * cball = new cannon(45,90,5);
This create an instance of cannon but i want multiple simultaneous instances by creating an array of them how'd i do that please?
-
Depends on what you want to do. Something like this for example:
Code:
const int max_balls = 5;
cannon *cballs[max_balls];
for(int i = 0; i < max_balls; i++)
cballs[i] = new cannon(45+i, 90+2*i, 5);
--
Mats
-
If you want an array of cannon objects:
Code:
cannon arr[] = {cannon(45, 90, 5), cannon(23, 23, 23), cannon(...)}; // array of 3 cannon object
If you want an array of pointers to cannon object, then:
Code:
cannon *arr[] = {new cannon(45, 90, 5), new cannon(23, 23, 23), new cannon(...)};
-
-
I would consider a vector<cannon>. You can't initialize each object inline to different values, but if they all have the same value you can initialize it on construction. If they don't you can just push_back each value one at a time.