references

I just got a bit confused at our last reading, i thought i understand references just fine but apparently I don't. I always thought this:

1
2
int a=5;
int &b=a;


is the same as:

1
2
3
int a=5;
int b;
&b=&a;


except the compiler is prompting you with ansi if you want to change adress of the b variable in the first case, it's just a warning though so it doesn't really matter... or does it?
I think you're confusing the address-of operator and the reference declarator. The reference declarator is just a syntactical feature and is completely unrelated to the operator. It could have have been $ (e.g. int $b=a;) and it wouldn't have changed anything.
The address-of operator, as its name implies, gives the address of its operand. The result is an rvalue, which means it can't be used as the left-hand operand of the assignment operator.

Your first snippet is equivalent to this:
1
2
int a=5;
int *b=&a;
ahaa.. wow, thanks a lot
Topic archived. No new replies allowed.