Re: Iterate through member variables of a class

From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 08/05/04


Date: Wed, 4 Aug 2004 23:11:42 -0400


"Stephen Tyndall" <swtyndall@hotmail.com> wrote in message
news:Hv-dnTPdNuLCPozcRVn-vQ@comcast.com...
> "bst" <bstom@import.com> wrote in message
> news:9WfQc.387998$Gx4.257975@bgtnsc04-news.ops.worldnet.att.net...
> > 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[i] << "\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


Relevant Pages

  • Re: export static public member
    ... DLLs cannot use it simultaneously. ... See this message for the proper way to ... I use this class and the public static member variables. ... link error of unresolved externals of the static public member variables. ...
    (microsoft.public.vc.mfc)
  • Re: Weird behavior on allocation...whats going on?
    ... A little feedback on your code: ... it's conventional to put your member variables right at the top of the ... Immutable objects also remove the chance to mangle objects that other ...
    (comp.lang.java.programmer)