References confuse me. How do they differ from pointers?
---------------------------------------------------------------------------------- A reference is an alias to a variable, object. They are merely alternate identifiers for the same object. Note that a variable and its reference can be used interchangeably. Hence they are synonyms of each other...Although they contain the address of the object, however they are more like Constant Pointers which cannot be altered.
Regular pointers can be manipulated, i.e. incremented; decremented which can also be
dangerous. However a
reference cannot be manipulated. Hence they are
safer to use because you are not accidentally changing the address of the variable .
cpp code
int actualint;
int& otherint = actaulint;
The real usefulness of references is when they are used to pass values into functions. They provide a way to return values from the function.The reference lets you pass and return large data structures without the overhead of copying them. A reference is also a way to avoid pointer dereferencing syntax in your code. The
& operator identifies a reference variable.