[LC++]can't seem to use reference
carlo at alinoe.com
Tue Feb 19 16:45:35 UTC 2002
On Tue, Feb 19, 2002 at 11:32:05AM +1030, Mark Phillips wrote:
> > largeStructTy const& ls(largeStructList[i]);
> > largeStructTy const& nextLs(largeStructList[i]);
> > nextLs = largeStructList[i + 1];
> any case, both out pieces of code fail because C++ doesn't allow
> the reassignment of a reference.
Excuse me, but I am not reassigning a reference, the line
"nextLs = largeStructList[i + 1]" is doing the copying
that I thought were doing. Thats what you get when you
write illegal code (nonsense thus).
> 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...
largeStructTy const* lsp = &largeStructList; // Single, one time assembly instruction.
for (largeStructTy const* next_lsp = &largeStructList; // 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
So, that is two assembly instructions inside the loop,
an increment of register and a compare of register
with constant (address).
Carlo Wood <carlo at alinoe.com>
More information about the tuxCPProgramming