473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Iterate through member variables of a class

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
10 8674
"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
"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
"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
"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
"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
"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
"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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
5
by: jaso | last post by:
Hi, If have a structure of a database record like this: struct record { char id; char title; ... }; Is there some way to find out how many member variables there is in the struct and then...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
3
by: toton | last post by:
Hi, I have a container class, and I want to iterate over a portion of the container class while I insert/remove item from it. Noting down the present location & constructing iterator from there is...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.