Re: REFERENCES REVEALED

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/17/04


Date: Mon, 17 May 2004 06:42:58 +0100


"JKop" <NULL@NULL.NULL> wrote in message
news:u9Ppc.8004$qP2.16286@news.indigo.ie...
>
> I understand variables/objects and pointer variables perfectly:
>
> int X = 5;
>
> int* pX = &X;
>
> *pX = 4;
>
> int** ppX = &pX:
>
> **ppX = 3;
>
>
> ---
>
> I know exactly how references "behave", but I want to know how they
actually
> work!
>
> For instance, take the following function:
>
> void Repot(unsigned int* pX)
> {
> *pX = 45U;
> }
>
>
> The caller will do the following:
>
> int main(void)
> {
> unsigned int X = 17U;
>
> Repot(&X);
> }
>
>
> Obviously one could rewrite the above code as:
>
>
> void Repot(unsigned int& X)
> {
> X = 45U;
> }
>
>
> int main(void)
> {
> unsigned int X = 17;
>
> Repot(X);
> }
>
>
> But what I want to know is what's actually going on under the hood. One
> quick little side note here though before I continue: When I first learned
> about references, I detested them, I thought they were extremely stupid.
One
> may argue that they're easier to use, just as one may argue that an
> automatic transmission car is easier to driver, but long story short, I
> prefer manual transmission and detest references. So anyway, I could find
no
> justification for the introduction of references into C++, other than to
> make operator overloading for classes possible. I was pleasantly amused
when
> I heard a quote from Bjarne himself stating that the reason he introduced
> references into C++ was to enable operator overloading for classes. Fair
> enough, and although in my opinion it is heavily contrived, it works! Just
> for clarification, here's what I hate so much about references: Before,
when
> calling a function, you could specify "X" to indicate the value stored in
> the variable, and you could specify "&X" to indicate the memory address of
> the variable. With references, this is no longer so. (Yes, I am aware that
> one can determine from the function prototype whether or not it's a
> reference... I don't like it).

You're missing out on two important aspect of references, one aesthetic, one
practical.

>From the aesthetic point of view the chief difference between pointers and
references is that references always refer to the same object, unline
pointers. So if you need to refer to some object, and that reference (in the
general sense) is never going to refer to another object during its lifetime
you should consider using a reference (in the C++ sense) because it better
expresses what you are doing.

>From the practical point of view there is the fact that a const reference
can bind to a temporary, something a pointer cannot do. Consider

class X;

void f1(const X*);
void f2(X);
void f3(const X&);
X g();

f1(&g()); // illegal

X t = g();
f1(&t); // ugly temp variable, extra copy

f2(g()); // extra copy, may or may not be optimised by compiler

f3(g()); // perfect

>
> So anyway, how does the compiler deal with references? Does it simply
treat
> them as short-hand? Would the compiler turn my preceeding code (written
> using references) back into pointers? ie. would it simply stick in the "*"
> and "&" where appropriate?

Almost certainly, so certain am I that I've always assumed this to be the
case on every compiler I've ever used, and haven't actually bothered to
check. The only way to be sure would be to have a look at the machine code
your compiler generates.

john