Iterate through member variables of a class

B

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!
 
V

Victor Bazarov

bst said:
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.
 
S

Stephen Tyndall

bst said:
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
 
D

David Hilsee

Stephen Tyndall said:
bst said:
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.
 
S

Siemel Naran

David Hilsee said:
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>.
 
S

Stephen Tyndall

David Hilsee said:
news:[email protected]... [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
 
S

Stephen Tyndall

Siemel Naran said:
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
 
D

David Hilsee

Stephen Tyndall said:
David Hilsee said:
news:[email protected]... [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.
 
M

Mike Tyndall

David Hilsee said:
Stephen Tyndall said:
David Hilsee said:
news:[email protected]... [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).
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
 
D

David Hilsee

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.
 
M

Mike Tyndall

David Hilsee said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top