Re: Iterate through member variables of a class

From: Mike Tyndall (swtyndall_at_hotmail.com)
Date: 08/06/04


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



Relevant Pages

  • Re: Comparing pointers to NULL
    ... Suppose our data is an array of records with each record having many ... In C it is natural to use a struct to represent a record and an ... in the body of a loop over record number the current record number ...
    (comp.lang.c)
  • Re: C code is not generating required results.
    ... int main ... the array by the size of an element. ... prevent the user of your program from entering more characters than ... want to loop. ...
    (comp.lang.c)
  • Re: Streaming files
    ... int lIndex = 0; ... Why are you opening and closing the file for each iteration of the loop? ... As you are increasing lIndex for every iteration in the loop, ... If you want to fit all that data into the array, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)
  • Re: Beginners prime number generator
    ... int main{ ... int nextprime = 0; ... is typically tested by being *less than* the length of the array. ... a sqrtcalculation in the outer loop and just compare counter ...
    (comp.lang.c)