CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    3

    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?

  2. #2
    Join Date
    Nov 2009
    Posts
    3

    Re: Easier accessing of objects

    Would that be a link list class?

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  4. #4
    Join Date
    Nov 2009
    Posts
    3

    Re: Easier accessing of objects

    Yep. Just tried an array of pointers before reading that.

  5. #5
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured