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

Mark Phillips mark at austrics.com.au
Tue Feb 19 12:04:01 UTC 2002


Carlo Wood wrote:
> 
> On Mon, Feb 18, 2002 at 11:03:06AM +1030, Mark Phillips wrote:
> > 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?
> 
> My 'solution' doesn't copy anything at all, there are only
> reference initializations.

You are right.  Silly me.  So the difference is that your method
calculates two addresses each time, whereas mine calculates only one new
address each time, getting the other address from the nextLs of the
previous iteration.  So my method is slightly more efficient (or would
be if it were legal C++), but only marginally so.

> Actually, I hadn't noticed that you copied data from 'i+1' to 'i';
> So I should have typed:
> 
> for (int i = 0; i < N; ++i)
> {
>   largeStructTy const& ls(largeStructList[i]);
>   largeStructTy const& nextLs(largeStructList[i]);
>   nextLs = largeStructList[i + 1];
>   // do stuff involving the reading of ls and nextLs
>   // ...
> }
> 
> which is equivalent with your pseudo code.

It's not quite equivalent, because mine constructed nextLs only once
and its reference is used to update ls, whereas yours constructs nextLs 
and then discards the reference in favor of the next one along.  In 
any case, both out pieces of code fail because C++ doesn't allow
the reassignment of a reference.

> I can assume that what you really want is this:
> Copy element i+1 to i, keep a reference to both, where
> reference ls corresponds to i and nextLs to i+1.

No I don't think that's what I mean... if I am understanding
what you are saying correctly.

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.

But the loss of efficiency involved in explicitly
calculating both references at each iteration, is probably
relatively minor, so perhaps this is the best thing to do.
My alternative it would seem, is to use pointers instead.

That is what I am trying to decide.  Should I work with references
or should I work with pointers?  If there are times where I'll have
to use pointers for this kind of thing, is it better that I use
pointers most places?... for consistency's sake.

By the way, thanks for your feedback.  I really appreciate it!

Cheers,

Mark.



More information about the tuxCPProgramming mailing list