Jump to content
  • Advertisement
Sign in to follow this  
flounder

looping through structs

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

Advertisement
indeed...do you mean loop through instance of a single struct or looping through all instances of all structs created......

the former...yes
the latter...no

assuming you mean looping through instances of a single struct and those instances are in an array or datastructure of some sort..then you can just run a for loop...if im way of base as far as what you were looking for....feel free to elaborate on your problem...

Share this post


Link to post
Share on other sites
Quote:
Original post by Tang of the Mountain
the latter...no

Actually, yes, it's totally possible. It's just totally non-trivial and certianly not language supported (and it's intrusive to the struct in question as well).

Share this post


Link to post
Share on other sites
i mean looping through all of the structs created

struct thingy
{
int x;
};
thingy thing1;
thingy thing2;
thingy thing3;

how would i be able to loop through the thingy's just created

Share this post


Link to post
Share on other sites
Quote:
Original post by flounder
i mean looping through all of the structs created

struct thingy
{
int x;
};
thingy thing1;
thingy thing2;
thingy thing3;

how would i be able to loop through the structs just created


Sounds like you want to store each "thingy" in a container of some sort, like an array or std::vector. Then yes, you can iterate through each element of the array/vector to do whatever to your "thingy"s.

Share this post


Link to post
Share on other sites
OK, then.

Well, it depends on exactly how you want to do it. For your purposes, you probably want to just make a vector or list. Like so:


// make a list of thingies
std::list<thingy> mythings;
mythings.push_back(thingy);
mythings.push_back(thingy);
mythings.push_back(thingy);

// loop through like this:
for(std::list<thingy>::iterator it = mythings.begin(); it != mythings.end() ++it)
{
// call a function
DoSomeThingyThing(*it);
// or perhaps change a value
it->x += 3;
}



To loop through all the thingies ever created in your program (generally you won't want to do this), you need to make some kind of static list as a member of thingy and add every thingy created to it. Again - the need to do this is rare - you probably don't want to do this.

Share this post


Link to post
Share on other sites
i can use arrays sucessfuly but the structs won't be created in the same spot like above. other structs will have it and it will be called/used in other peices of code.

i've been thinking of creating an array that points to the struct and then looping through the array of pointers. they would have to call some function to make sure an instance of the struct is included in one of the array instances. however i can't seem to get it to work.

here's an example of what i thought:


struct thingy
{
int x;
};

thingy*&thingy_pointer; //an array of pointers
int num_pointers=10,current_pointer;
thingy_pointer=new thingy[num_pointers];

thingy thing1;
register_thingy(thing1);

//make sure something in the array points to an instance of the struct
void register_thingy(thingy &a)
{
thingy_pointer[current_pointer]=a;
current_pointer++;
}

//loop through the array of pointers
for (int i=0;i<num_pointer;i++)
{
//modify the pointer which will modify the original struct
thingy_pointer.x=1;
}


Share this post


Link to post
Share on other sites
sorry inmate, i need an array of pointers . this way, the array can have its pointers set to calls of the struct and then i can loop through the pointers.

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!