reference or address of?

Hello, I had found a post on here a while back explaining how to know the difference between these two, but i can not seem to find it now. I think i remember what was explained but just wanted to double check it, could someone please correct me if my thoughts on it is wrong.

So from what I remember it is a reference if the & is on the left side of the = or if it follows a type name (before the variable name). ie - int &x

And it is a address if the & is on the right of the =

is this correct?
It means reference if it's part of a type.
= has nothing to do with it.
Last edited on
x = &y; //x must be a pointer, getting the address of y
type &x = y; //x is a reference to y.

you can deduce a rule of thumb from above about left and right, and it will be mostly true.
I am having trouble thinking up the examples where that can go wrong, but learning to read it will do you better than a rule of thumb in the long run.
Last edited on
The rule is this:

- if it's used as part of the type when declaring/defining an entity, then it's a reference

1
2
int a = 1;
int& b = a;  // b is a reference to a 


- if it's used as a unary operator, it's the "address-of" operator

1
2
int a = 1;
int* c = &a;  // c stores the address of a 


- if it's used as a binary operator, it's the "bitwise and" operator

1
2
3
4
int a = 0x01;
int b = 0x03;

int c = a & b;  // c is 0x01 
Topic archived. No new replies allowed.