Advertisement Jump to content
Sign in to follow this  

C++ question

This topic is 5056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello How can I create multiple objects and put them in an array in a for loop? lets say I have a object array called: object_array of object3d kind. object3d *object_array [NUM_OBJECTS]; and lets say I can create one object of particle3d kind called part1 as follow: particle3d *part1 = new particle3d (0.4,0,0, 0); part1->setPosition(*(new vec3(-10,10,0))); object_array[1]=part1; now I dont know how I can create multiple of the part1 object and then put it in object_array inside a for loop. I tried this but didnt work, :( any idea? for (i=0;i<=100;j++) { particle3d *part+i = new particle3d (0.4,0,0); . . . } thank you

Share this post

Link to post
Share on other sites
particle3d * part;
object3d object_array[100];

for (unsigned i=0; i<100; ++i)
part = new particle3d(0.4, 0.0, 0.0);
object_array = part;

Should work (your code isn't very clear, please try indenting and using
 tags) as long as an object3d can be a particle3d.  If not, it wont work, of course.

Share this post

Link to post
Share on other sites
ok you are correct.
lets say I have a class for particle3d, which itself is an inherited version of object3d.
then I create an array of object3d that can hold many particle3d.
each of these particle3d can have different weight, speed, etc therfore they should be different with one and another (they have different values).
now I only want to put all of these in that array automatically instead of :

particle3d *part3 = new particle3d (0.4,0,1, 0);
part3->setPosition(*(new vec3(-4,10,0)));

particle3d *part4 = new particle3d (0.4,0,1, 0);
part4->setPosition(*(new vec3(-1,10,0)));

as you can see part4 position is different with part3.
what do you think now?
thank you

Share this post

Link to post
Share on other sites
part3 and part4 can be replaced by a single part variable if it is dynamically allocated because it is only a pointer to the object; the actual object being placed in the array is independent of this reference.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!