loop for multiple objects

Started by
8 comments, last by boogyman19946 13 years, 9 months ago
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.
Advertisement
Why are they not in some kind of container?
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
it would be very hard near impossible to use an array unforchanatly , this is why i need to try to do it another way
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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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. :)
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.
Maybe the OP is a user of 4chan?
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.
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).

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement