Jump to content
  • Advertisement
Sign in to follow this  
thedodgeruk

loop for multiple objects

This topic is 3006 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

i want to create a loop for multiple objetts that are not in an array ie



class foo
{
void run();
}


foo train,bus , car person, cat ;


for (int i = 0 ; i < 5; i++)
{
// all the foo's one by one
}


can something be put in the class like a static counter that can be refered to instead of the name (train) ect.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by thedodgeruk
i want to create a loop for multiple objetts that are not in an array ie



class foo
{
void run();
}


foo train,bus , car person, cat ;


for (int i = 0 ; i < 5; i++)
{
// all the foo's one by one
}


can something be put in the class like a static counter that can be refered to instead of the name (train) ect.


Not sure if this is what you mean.. but lets see if this is it:



class foo
{
public:
givenumber();
returnnumber();
move();
private:
int number;
};

foo train, bus, cat, idiot;

foo::returnnumber(){ return number; }
foo::givenumber( int number_given ){ number = number_given; }


// this is in some function..
for ( int loop = 0; loop < 5; loop++ )
{
if ( train.returnnumber() == loop )
{
train.move();
}

if ( bus.returnnumber() == loop )
{
bus.move();
}

//rest here
}







but that would be stupid to do.

What I would use is something like that, but then as an array:


class foo
{
public:
settype();
move();
draw();
private:
int type;
};

foo myUnits[5];

foo::returnnumber(){ return type; }
foo::givenumber( int type_given ){ type= type_given; }


// this is in some function..
for ( int loop = 0; loop < 5; loop++ )
{
foo[loop].move();
}

// and then in the draw function:
foo::draw()
{
if ( type == 0 ){ //draw train }
else if (type == 1 ){ //draw bus }
//etc..
}




I mean, I don't get it - why don't you want to use an array? :S

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Why are they not in some kind of container?


Second'd. A container of type Foo would let you iterate through all objects without having to give each object some kind of numerical tag.

Share this post


Link to post
Share on other sites
Quote:
Original post by thedodgeruk
it would be very hard near impossible to use an array unforchanatly , this is why i need to try to do it another way


You'll probably have to give us more details about your situation. Why can you not use an array? How about an array of pointers?

Boy, "unforchanatly" is some of the most creative spellings I've ever seen. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
Boy, "unforchanatly" is some of the most creative spellings I've ever seen. :)


Lol I thought exactly the same. also 'objetts' is also very creative.

Share this post


Link to post
Share on other sites
I agree with the other posters, you should store your objects in some type of container before trying to iterate through them to perform function calls.

Share this post


Link to post
Share on other sites
Maybe try using a switch statement and an internal tag. The way I see it, if you have a specific number of objects, you can create an enumerator and just enumerate through them. Take this for example:


enum classID {
orange,
apple,
banana
};

class Example
{
public:
// stuff

classID mID;
private:
// other stuff
};

int main() {
Example example1, example2, example3;
example1.mID = orange;
example2.mID = apple;
example3.mID = banana;

Example[] exmpArr = {example1, example2, example3};

for (int i = 0; i < 3; i++) {
switch(exmpArr.mID)
{
case orange:
// some stuff here
break;
case apple:
// some more stuff here
break;
case banana:
// and finally a bit over here
break;
}
}
}



Of course this creates a problem if you have a lot of classes that you are working with because you can't make the assignment automatic unless all the classes are the different from each other: you can do all of that in a loop since enumerating constants are assigned ascending integer values. Overall, it's a pain in the ass, but it'll work and I believe it may accomplish what you're looking for. If you want to combined some stuff, all you have to do is set up some if statements for the break; command or I guess you could work with goto statements which I really don't recommend, but I don't think you're code will become so bad of spaghetti code in this case (as long as you use some proper labels).

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!