Iterate through member variables of a class

Discussion in 'C++' started by bst, Aug 5, 2004.

  1. bst

    bst Guest

    Is there a way to iterate through member variables of different names, of
    course, one by one from the very first one in a while loop, for example?
    Thanks!
     
    bst, Aug 5, 2004
    #1
    1. Advertising

  2. "bst" <> wrote...
    > Is there a way to iterate through member variables of different names, of
    > course, one by one from the very first one in a while loop, for example?


    No.
     
    Victor Bazarov, Aug 5, 2004
    #2
    1. Advertising

  3. "bst" <> wrote in message
    news:9WfQc.387998$...
    > Is there a way to iterate through member variables of different names, of
    > course, one by one from the very first one in a while loop, for example?
    > Thanks!


    I'm probably doing something stupid, but did you mean something like this?
    Even if something's wrong with it (and there probably is), at least I'll
    learn something when everyone corrects me :)

    One thing I do know that's wrong is the public member variables in both
    classes, but I left those in so that I could fool around with it (and I
    can't think of any other way to make this setup work).

    Provided they work, the classes could be altered to be templates instead.

    #include <iostream>

    using std::cout;

    class ReadMe {

    public:

    ReadMe(int num1, int num2, int num3, int num4) {

    var1 = num1;

    var2 = num2;

    var3 = num3;

    var4 = num4;

    }

    int var1;

    int var2;

    int var3;

    int var4;

    };

    class Reader {

    public:

    Reader(ReadMe& readFromThis) : target(readFromThis) { }

    void printTargetVars() const;

    ReadMe& target;

    };

    void Reader::printTargetVars() const {

    int* arr[4] = { &target.var1, &target.var2, &target.var3, &target.var4 };

    int i = 0;

    while(i < 4) {

    cout << *arr << "\n";

    ++i;

    }

    }

    int main() {

    ReadMe readThisOne(1,2,3,4);

    ReadMe readThisTwo(5,6,7,8);

    Reader reader1(readThisOne);

    Reader reader2(readThisTwo);

    reader1.printTargetVars();

    reader2.printTargetVars();

    return 0;

    }

    //mike tyndall
     
    Stephen Tyndall, Aug 5, 2004
    #3
  4. bst

    David Hilsee Guest

    "Stephen Tyndall" <> wrote in message
    news:...
    > "bst" <> wrote in message
    > news:9WfQc.387998$...
    > > Is there a way to iterate through member variables of different names,

    of
    > > course, one by one from the very first one in a while loop, for example?
    > > Thanks!

    >
    > I'm probably doing something stupid, but did you mean something like this?
    > Even if something's wrong with it (and there probably is), at least I'll
    > learn something when everyone corrects me :)
    >
    > One thing I do know that's wrong is the public member variables in both
    > classes, but I left those in so that I could fool around with it (and I
    > can't think of any other way to make this setup work).

    [snip]
    > class Reader {
    >
    > public:
    >
    > Reader(ReadMe& readFromThis) : target(readFromThis) { }
    >
    > void printTargetVars() const;
    >
    > ReadMe& target;
    >
    > };
    >
    > void Reader::printTargetVars() const {
    >
    > int* arr[4] = { &target.var1, &target.var2, &target.var3, &target.var4 };
    >
    > int i = 0;
    >
    > while(i < 4) {
    >
    > cout << *arr << "\n";
    >
    > ++i;
    >
    > }

    [snip]

    Looks OK to me, except for the public members and a few minor tweaks that
    could be made. Another option is to forego individual members and just use
    an array from the start.

    --
    David Hilsee
     
    David Hilsee, Aug 5, 2004
    #4
  5. bst

    Siemel Naran Guest

    "David Hilsee" <> wrote in message

    > Looks OK to me, except for the public members and a few minor tweaks that
    > could be made. Another option is to forego individual members and just

    use
    > an array from the start.


    How about a map? As in <string variable_name, int variable_value>.
     
    Siemel Naran, Aug 5, 2004
    #5
  6. "David Hilsee" <> wrote in message
    news:...
    > "Stephen Tyndall" <> wrote in message
    > news:...

    [code snipped]
    > Looks OK to me, except for the public members and a few minor tweaks that
    > could be made.


    If you have the time, could you tell me what tweaks? I'm kind of a beginner
    (2 months or so of programming now), and I'm trying to teach myself good
    programming habits.

    > Another option is to forego individual members and just use
    > an array from the start.


    Huh. I didn't even think of that. Guess I've learned something then :)

    //mike tyndall
     
    Stephen Tyndall, Aug 5, 2004
    #6
  7. "Siemel Naran" <> wrote in message
    news:6QhQc.388568$...
    > "David Hilsee" <> wrote in message
    >
    > > Looks OK to me, except for the public members and a few minor tweaks

    that
    > > could be made. Another option is to forego individual members and just
    > > use an array from the start.

    >
    > How about a map? As in <string variable_name, int variable_value>.


    I haven't learned maps yet, so I'll have to look into them. Thanks for the
    tip!

    //mike tyndall
     
    Stephen Tyndall, Aug 5, 2004
    #7
  8. bst

    David Hilsee Guest

    "Stephen Tyndall" <> wrote in message
    news:...
    > "David Hilsee" <> wrote in message
    > news:...
    > > "Stephen Tyndall" <> wrote in message
    > > news:...

    > [code snipped]
    > > Looks OK to me, except for the public members and a few minor tweaks

    that
    > > could be made.

    >
    > If you have the time, could you tell me what tweaks? I'm kind of a

    beginner
    > (2 months or so of programming now), and I'm trying to teach myself good
    > programming habits.


    It's nothing, really. One point was that you used a while when a for would
    have been more natural. The other point was that you could have used the
    sizeof() "trick" to avoid using the literal 4 in your code.

    int* arr[] = { &target.var1, &target.var2, &target.var3, &target.var4 };
    int numElems = sizeof(arr) / sizeof(arr[0]);

    for ( int i = 0; i < numElems; ++i ) {
    cout << *arr << "\n";
    }

    Thanks to sizeof(), you can add or remove elements from the array and the
    other code doesn't have to change. It comes in handy more often in C than
    it does in C++, because in C code it is more likely to have an array whose
    size can be determined by the compiler. I don't know if you've seen that
    before or not, so there it is. Like I said, minor tweaks. To be completely
    anal, the int is being used to iterate over an array whose length is defined
    in terms of std::size_t, but that's far too picky for my tastes.

    --
    David Hilsee
     
    David Hilsee, Aug 6, 2004
    #8
  9. bst

    Mike Tyndall Guest

    "David Hilsee" <> wrote in message
    news:...
    > "Stephen Tyndall" <> wrote in message
    > news:...
    > > "David Hilsee" <> wrote in message
    > > news:...
    > > > "Stephen Tyndall" <> wrote in message
    > > > news:...

    > > [code snipped]
    > > > Looks OK to me, except for the public members and a few minor tweaks
    > > > that could be made.


    Just a note: I didn't realize that the Reader class' ReadMe& didn't need to
    be public. That's fixed now; I also rewrote both classes as templates and
    added a new template class that can read two ReadMe's of differing types
    (yes, I'm still messing with it).

    > >
    > > If you have the time, could you tell me what tweaks? I'm kind of a
    > > beginner (2 months or so of programming now), and I'm trying to teach
    > > myself good programming habits.

    >
    > It's nothing, really. One point was that you used a while when a for

    would
    > have been more natural.


    I did that because the OP was asking about iterating through member
    variables of a class by using a while loop. I generally prefer for loops.

    > The other point was that you could have used the
    > sizeof() "trick" to avoid using the literal 4 in your code.
    >
    > int* arr[] = { &target.var1, &target.var2, &target.var3, &target.var4 };
    > int numElems = sizeof(arr) / sizeof(arr[0]);
    >
    > for ( int i = 0; i < numElems; ++i ) {
    > cout << *arr << "\n";
    > }
    >
    > Thanks to sizeof(), you can add or remove elements from the array and the
    > other code doesn't have to change. It comes in handy more often in C than
    > it does in C++, because in C code it is more likely to have an array whose
    > size can be determined by the compiler. I don't know if you've seen that
    > before or not, so there it is. Like I said, minor tweaks.


    I didn't know this one. That's pretty clever (to me, anyway)!

    > To be completely
    > anal, the int is being used to iterate over an array whose length is

    defined
    > in terms of std::size_t, but that's far too picky for my tastes.


    So it would be better if I declared the int as a size_t instead? Or does it
    matter? Thanks for your time.

    //mike tyndall, finally posting as myself
     
    Mike Tyndall, Aug 6, 2004
    #9
  10. bst

    David Hilsee Guest

    "Mike Tyndall" <> wrote in message
    news:p...
    <snip>
    > So it would be better if I declared the int as a size_t instead? Or does

    it
    > matter? Thanks for your time.


    No, don't bother. It's unsigned, and that opens up a can of worms if you're
    not paying attention. Just keep in mind that you may see code that uses
    std::size_t instead.

    --
    David Hilsee
     
    David Hilsee, Aug 6, 2004
    #10
  11. bst

    Mike Tyndall Guest

    "David Hilsee" <> wrote in message
    news:...
    > "Mike Tyndall" <> wrote in message
    > news:p...
    > <snip>
    > > So it would be better if I declared the int as a size_t instead? Or does
    > > it matter? Thanks for your time.

    >
    > No, don't bother. It's unsigned, and that opens up a can of worms if

    you're
    > not paying attention. Just keep in mind that you may see code that uses
    > std::size_t instead.


    Gotcha. Thanks again.

    //mike tyndall
     
    Mike Tyndall, Aug 6, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,131
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,483
    runescience
    Feb 9, 2006
  3. Steve

    Iterate through loop of variables

    Steve, Jul 10, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    305
    Juan T. Llibre
    Jul 10, 2007
  4. John
    Replies:
    4
    Views:
    948
    RedGrittyBrick
    Apr 1, 2008
  5. MarkB
    Replies:
    4
    Views:
    327
    Sarkar
    Aug 7, 2009
Loading...

Share This Page