Re: Iterate through member variables of a class
From: Mike Tyndall (swtyndall_at_hotmail.com)
Date: 08/06/04
- Next message: red floyd: "std::fill and containers of pointers"
- Previous message: puppet_sock_at_hotmail.com: "Re: VC++6 missing storage-class or type specifiers"
- In reply to: David Hilsee: "Re: Iterate through member variables of a class"
- Next in thread: David Hilsee: "Re: Iterate through member variables of a class"
- Reply: David Hilsee: "Re: Iterate through member variables of a class"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 6 Aug 2004 12:56:39 -0500
"David Hilsee" <davidhilseenews@yahoo.com> wrote in message
news:UYSdnRJyJO9fWo_cRVn-qQ@comcast.com...
> "Stephen Tyndall" <swtyndall@hotmail.com> wrote in message
> news:yvWdnT3-WJhEKozcRVn-gQ@comcast.com...
> > "David Hilsee" <davidhilseenews@yahoo.com> wrote in message
> > news:OcSdnQiWnM3FOIzcRVn-pA@comcast.com...
> > > "Stephen Tyndall" <swtyndall@hotmail.com> wrote in message
> > > news:Hv-dnTPdNuLCPozcRVn-vQ@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
- Next message: red floyd: "std::fill and containers of pointers"
- Previous message: puppet_sock_at_hotmail.com: "Re: VC++6 missing storage-class or type specifiers"
- In reply to: David Hilsee: "Re: Iterate through member variables of a class"
- Next in thread: David Hilsee: "Re: Iterate through member variables of a class"
- Reply: David Hilsee: "Re: Iterate through member variables of a class"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|