[LC++]can't seem to use reference
    Mark Phillips 
    mark at austrics.com.au
       
    Wed Feb 20 12:34:38 UTC 2002
    
    
  
Carlo Wood wrote:
> 
> > I am considering largeStructList to be a read-only array of
> > large structures.  At each stage in the for loop I want to
> > process things involving consecutive pairs.  I could, as you
> > say, explicitly instantiate references to the two consecutive
> > objects at each iteration.  But a better way would be to use
> > the second reference at iteration i, as the first reference
> > at iteration i+1, meaning there is no need to recalculate
> > it.  Unfortunately C++ syntax does not allow this.
> 
> Well, now I know what you want...
Sorry if I wasn't clear enough before.
> 
> largeStructTy const* lsp = &largeStructList[0];                 // Single, one time assembly instruction.
> for (largeStructTy const* next_lsp = &largeStructList[1];       // Single, one time assembly instruction.
>      next_lsp < &largeStructList[N];                            // &largeStructList[N] is a constant, compared with a register.
>      ++next_lsp)                                                // Single assembly instruction.
> {
>   largeStructTy const& ls(*lsp);                // Generates no code, internally lsp and lp are the same register.
>   largeStructTy const& nextLs(*next_lsp);       // Idem, generates no code.
>   // do stuff involving the reading of ls and nextLs
>   // ...
> }
Thanks for this.  I didn't think of doing it like that.  It's still
a little messy but I guess in C++ you don't have a choice (other than
to write less efficient code).
Thanks again for your feedback.
Cheers,
Mark.
    
    
More information about the tuxCPProgramming
mailing list