C++ question

Started by
3 comments, last by bargasteh 19 years, 1 month ago
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
OpenGl + C++
Advertisement
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.
my siteGenius is 1% inspiration and 99% perspiration
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)));	object_array[3]=part3;	particle3d *part4 = new particle3d (0.4,0,1, 0);	part4->setPosition(*(new vec3(-1,10,0)));	object_array[4]=part4;


as you can see part4 position is different with part3.
what do you think now?
thank you
OpenGl + C++
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.
Thank you very much, I got it now.
OpenGl + C++

This topic is closed to new replies.

Advertisement