@webJose
The "reference sign" is the "Address Of" operator. |
No, they are both ampersands, but they have completely different purposes. You use the reference operator to declare a reference variable (similarly to the * for pointers).
1 2 3
|
int a = 10;
int *p = &a; //declaring a pointer to the address of a
int &r = a; //declaring a reference to a
|
You can change the value of a through both p and r. But p is a pointer whilst r is a reference.
Pointers and reference are similar, but have different behavior, property and syntax.
@Rox
You can pass a variable three ways in C++.
By value, by reference and by pointer.
Passing by value means, the variable gets copied thus you can't change the original passed variable. (An example for this is your second code).
Passing by reference and by pointer means the variable you pass doesn't get copied, instead a pointer* is passed to the function, this way we save the cost of copying (for large objects), and we can modify the original variable. (Your first code is an example for passing by reference). References have simpler syntax, since one doesn't have to dereference it explicitly every time, but has stricter rules.
*a reference is basically a pointer too.
http://www.cplusplus.com/doc/tutorial/pointers/