Jump to content
  • Advertisement
Sign in to follow this  
Xiachunyi

C++:Structure access in a loop

This topic is 4862 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, I was wondering if it was possible to access a structure's members via a loop without having to manually stipulate the names of the members. For example:
struct
{
    bool foo1;
    bool hi;
    bool bye;
} stuff;
...
for(int loop=0; loop < 3; loop++)
{
    if(stuff.?) //<-This has to change from "foo" to "hi" and finally "bye" so each one could be evaluated.
    {
        //Do stuff
    }
}


I am wondering if I must use something such as pointer manipulation but I am not quite sure. I could just make an array but that would probably confuse me later on when I am re-editing my code. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You could use a utility function and pass the appropriate variable by reference...


struct
{
bool foo1;
bool hi;
bool bye;
} stuff;


void func(bool &x)
{
// do stuff...
}

...

for(int loop=0; loop < 3; loop++)
{
func(stuff.foo);
func(stuff.hi);
func(stuff.bye);
}


I hope I interpreted you correctly...

-- Steven Ashley

Share this post


Link to post
Share on other sites
If you know the imprint of the structure (i.e what types are in what order), then you could do this with pointer manipulation. Otherwise you're in a bind.

Share this post


Link to post
Share on other sites
Sorry, my C++ is a little rusty. Maybe someone else can tell you for sure, but is it possible to overload an indexer in C++?

Share this post


Link to post
Share on other sites
This should work:


bool *b = (bool*)&stuff;
for(int i=0; i<3; i++)
{
// read/write using "b" (without quotes)
}





I wouldn't reccomend it (what if you ever change "stuff"), also I don't really see the point to not using an array of bools, but still, it whould work...

Share this post


Link to post
Share on other sites
You should use a union. Something like

struct stuff
{
union
{
bool b[3];
struct
{
bool fool;
bool hi;
bool bye;
}
}
};



tj963

Share this post


Link to post
Share on other sites
Another solution:

const bool stuff::* const foo[3] = {&stuff::fool, &stuff::hi, &stuff::bye};
for(int i=0; i < 3; i++){
if(foo){
//Do stuff
}
}

Share this post


Link to post
Share on other sites
Good set of replies and solutions. The main reason I am attempting this is because I am trying to allocate an options section in my program.

All of the options are stored in an options structure and cycling through the options structure will provide the necessary state for the check boxes in my program's options dialog box.

I guess I could just go ahead and make an array of bools and just write comments on which so I will not become confused as which one is which later on.

Thanks to everyone who have contributed.

Share this post


Link to post
Share on other sites
array of bools, indexed by an enumeration which will serve to document what goes in each location.

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!