By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Iterate through member variables of a class

P: n/a
bst
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!
Jul 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"bst" <bs***@import.com> 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.
Jul 22 '05 #2

P: n/a
"bst" <bs***@import.com> wrote in message
news:9W*********************@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).

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[i] << "\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
Jul 22 '05 #3

P: n/a
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:Hv********************@comcast.com...
"bst" <bs***@import.com> wrote in message
news:9W*********************@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
Jul 22 '05 #4

P: n/a
"David Hilsee" <da*************@yahoo.com> 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>.
Jul 22 '05 #5

P: n/a
"David Hilsee" <da*************@yahoo.com> wrote in message
news:Oc********************@comcast.com...
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:Hv********************@comcast.com... [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
Jul 22 '05 #6

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:6Q*********************@bgtnsc04-news.ops.worldnet.att.net...
"David Hilsee" <da*************@yahoo.com> 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
Jul 22 '05 #7

P: n/a
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:yv********************@comcast.com...
"David Hilsee" <da*************@yahoo.com> wrote in message
news:Oc********************@comcast.com...
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:Hv********************@comcast.com... [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[i] << "\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
Jul 22 '05 #8

P: n/a
"David Hilsee" <da*************@yahoo.com> wrote in message
news:UY********************@comcast.com...
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:yv********************@comcast.com...
"David Hilsee" <da*************@yahoo.com> wrote in message
news:Oc********************@comcast.com...
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:Hv********************@comcast.com... [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[i] << "\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
Jul 22 '05 #9

P: n/a
"Mike Tyndall" <sw*******@hotmail.com> wrote in message
news:Pr********************@comcast.com...
<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
Jul 22 '05 #10

P: n/a
"David Hilsee" <da*************@yahoo.com> wrote in message
news:Ed********************@comcast.com...
"Mike Tyndall" <sw*******@hotmail.com> wrote in message
news:Pr********************@comcast.com...
<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
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.