-
November 13th, 2009, 05:59 PM
#1
Easier accessing of objects
Is it possible to go through all the objects in a class/structure and change their variables without using the names of all the objects?
Let's say I have a class/structure, fruit, with 500 objects:
Apple
498 objects
Cherry
With integers:
Yum
Size
If I wanted to change all their yums based on their size, that would require me to do something like:
Code:
Apple.yum*=size
Strawberry.yum*=size
...
Cherry.yum*=size
And that is way too much typing.
I've tried using a pointer to apple and then increasing it by 1. That seems to only work in arrays.
Yes, I am aware I could just make a giant array.
I have asked several people this question. Is it even possible?
-
November 13th, 2009, 06:08 PM
#2
Re: Easier accessing of objects
Would that be a link list class?
-
November 13th, 2009, 06:22 PM
#3
Re: Easier accessing of objects
You'd need to have some kind of container of objects or pointers to objects. A linked list would be one such container. An array would probably be a better one.
-
November 13th, 2009, 06:30 PM
#4
Re: Easier accessing of objects
Yep. Just tried an array of pointers before reading that.
-
November 13th, 2009, 08:15 PM
#5
Re: Easier accessing of objects
C++ offers no way to enumerate members of an object in the way that a language such as Actionscript or PHP does. Such things are really limated to the scripting languages where objects are implamented as hash maps behind the scenes. If you need to do so, the only options open to you are using an array or list instead of the object or a big array/list of pointers.
In theory there'd be no reason why incramenting a pointer wouldnt work if every element was the same size and you knew how many there were. No doubt it would be "undefined behaviour" and therefore be inadvisable.
Last edited by couling; November 13th, 2009 at 08:18 PM.
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 13th, 2009, 10:43 PM
#6
Re: Easier accessing of objects
Supposedly you can do something like this using Boost.Fusion, but it all seems a bit like Voodoo to me, so I'm not sure of the specifics.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|