meaning of"&"

Pages: 12
float & function(parameters)
what does the following syntax returns as result in other words what does the "&" stand for
thanks
It means it is returning a reference to a float.
what's wrong if i write float function(parameters) instead
float function(param) returns a copy of the float
float& function(param) returns a reference to a float

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float global_var = 5;
float get_global_var(){
    return global_var;
}

float& get_global_var_reference(){
    return global_var;
}

int main(){
    float var1 = get_global_var();
    var1++;//Now var1 = 6 and global_var = 5
    //var1 is only a copy of global_var

    float& var2 = get_global_var_reference();
    var2++;//Now both var2 and global_var are equal to 6.
    //var2 is a reference to global_var. It's similar to a pointer, if you understand those better

    return 0;
}
Last edited on
Keep in mind that returning a reference to a variable defined in function scope usually goes over really badly.
Is it me, or is this question being asked a TON lately?
Lol, I've been coding in c++ for 5 years and i STILL don't fully understand the & operator in pointer expressions :P
o_O really?

Anything specific you need clarifying?
Disch: not just you
meh, I've just never understood when to use it :O I know it's textbook deffinition, that it is a reference to the address in memory of where that particular variable, function, class what have you is. But i've never really understood how and when to use it
Well a reference is like a pointer in a lot of respects, except that (and I bet I'm missing some stuff)
a) it can't be rebound. You can change the target of a pointer as much as you like but you can't do the same thing with a reference variable. Once a reference is bound against something you can't change it.
b) You don't need to use addressing and dereferencing with a reference. To declare and use a pointer:
1
2
pointer = &foo; // need to refer specifically to the address of foo, not its value
pointer* = somevalue; // need to derefence to modify the value being pointed at, not the pointer itself 

A reference, on the other hand, cannot be rebound, and there is thus no distinction to be made between changing its target and changing the value of its target, because you can't change the target. So you don't need to mess with the deref operators:
1
2
3
reference = &foo; // you still need to make this qualification afaik
reference = somevalue; // when modifying a reference you automatically modify its target,
//because the assignment could not be to its address - references cannot be rebound 

Anything important I missed? (I don't actually remember whether you need to qualify the ref target with the address operator.)
Last edited on
I don't really like the reference<->pointer comparison. The two are different concepts.

But whatever. It seems like I've already explained my thoughts on that a dozen times, and I'm too tired to do it all again XD
Ooh I wanna read that.
@tummychow,
You assign a reference like this:
int& my_reference = my_int;
You don't have to do anything else. You can just pretend my_reference is my_int.
Yup, I had a feeling that I was forgetting something.
reference is where I'm at meaning address so when ampersand is use u know it is where i'm at so then you should conceive it as popback() when used meaning go back where i'm always at. pointer signify the heap or stack so in return it signify remember where im at, if i ever cant be found so reference is only understood by the programmer or an expert programmer because the location of memory does not change. it is the implication of the function you want used some where else took from the location which is its memory location, but this is only for the contrasting for a beginner because every detailed fact once again comes in play when your coding about pointers and referencing.
Last edited on
What??
The two are different concepts.

What do you mean? They do compile into the same machine code, I believe.
Last edited on
What do you mean? They do compile into the same machine code, I believe.

So do inline functions and macros.
So do virtual functions and function pointers.
So do static member functions and global functions.
So do namespaces and lack-of-namespaces
etc

Even if they're implemented the same way, they're two distinctly different concepts.

A pointer is a seperate variable which points to another variable (or to the beginning of an array of variables)
A reference is not a seperate variable, but is the same variable.

1
2
3
int foo;
int* ptr = &foo;  // ptr "points to" foo.  'ptr' is a seperate variable from 'foo'
int& ref = foo;  // ref IS foo.  They're one and the same.  Different names for the same variable. 
Last edited on
A reference is not a seperate variable
It might be.
Pages: 12