[LC++]can't seem to use reference

Mark Phillips mark at austrics.com.au
Mon Feb 18 11:27:51 UTC 2002


Carlo Wood wrote:
> 
> On Tue, Feb 12, 2002 at 01:27:31PM +1030, Mark Phillips wrote:
> > largeStructTy const& nextLs=largeStructList[0];
> > for (int i=0; i<N; ++i) {
> >   largeStructTy const& ls=nextLs;
> >   nextLs=largeStructList[i+1];
> >   // do stuff involving the reading of ls and nextLs
> >   // ...
> > }
> 
> int next=0;
> for (int i=0; i<N; ++i) {
>   largeStructTy const& ls=largeStructList[next];
>   largeStructTy const& nextLs=largeStructList[(next=i+1)];
>   // do stuff involving the reading of ls and nextLs
>   // ...
> }

Thanks for this solution.  My only concern with it is the
possibility of inefficiency.  My solution (if it were legal C++)
would only require one large structure to be read from memory
each iteration (the rest is simply pointer swapping).  Your
solution requires two large structures to be read each iteration.
Or would the compiler optimize this away?

Cheers,

Mark.

P.S. Is there a particular reason why you didn't write:

int i=0;
for (; i<N; ++i) {
  largeStructTy const& ls=largeStructList[i];
  largeStructTy const& nextLs=largeStructList[i+1];
  // do stuff involving the reading of ls and nextLs
  // ...
}

Ie, why the use of "next"?



More information about the tuxCPProgramming mailing list