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;
}
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.)
@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.
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.
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.