Re: C++ programmer and assembly
Andrei Alexandrescu (See Website For Email) wrote:
Francis Glassborow wrote:
Le Chaud Lapin wrote:
[snip]
And every one of those is specific to the meaning of reference in C++.
Of course you can initialize a reference to null we just do not allow
it in C++. Of course you can assign to a reference it is just that in
C++ this results in assigning to the referenced object.
It is just that C++ does not allow you to handle references as any
form of object so you cannot have a container of references in C++.
In all five of the above the important thing is understanding what a
C++ reference is. It has nothing whatsoever to do with machine code.
However they are reasons why the naive description of references as
just syntactic sugar for pointers is unhelpful. Things are not their
implementation. The fact that references are usually implemented as
hidden pointers has _nothing_ to do with the semantics of a reference.
Indeed this is an excellent example of knowing low level details being
a handicap to understanding.
Ionno. To me, our favorite field is a scale-free space in which
knowledge at all levels simply allows you to better understand what's
going on at all other levels. In contrast, architecture and various
engineering disciplines rarely need to span the range from atom to large
structure. But I'm on a limb here.
Here's a contrived section of code I had in a talk I gave at ACCU:
T1 Widget::fun() {
...
T2 & temp = *p;
return &temp;
}
The temp variable was introduced to prevent ambiguities in a nontrivial
inheritance hierarchy. An astute attendee noticed that the code is
illegal because I'm returning the address of a temporary. Initially I
thought he's right, but after a second look, I figured that the code is
correct because temp is essentially an "always dereferenced" pointer, so
the code is equivalent to:
T1 Widget::fun() {
...
T2 * pTemp = p;
return &*pTemp;
}
I could have also inferred that the code is correct through other means,
but the simple understanding that references are pointers with a "*"
always stuck to them made it easy to figure things out.
Interesting; to me, the pointer explanation was more
convoluted that noting that you are not returning the
address of a local, you're returning the address of
(an alias of) *p, which is p.
I don't point this out to refute your explanation; to
me, it shows only that different mental models are
more/less helpful to different individuals. Which
should come as no surprise. My teaching experience
has been that this is true much more generally, i.e.,
that people differ in which explanation they find
"obvious" or even understandable. One-size-fits-all
doesn't work.
-- James
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]