In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object (similar to rename keyword in Ada).
The declaration of the form:
where <Type> is a type and <Name> is an identifier whose type is reference to <Type>.
Examples:
Here, rA and rB are of type "reference to int"
foo() is a function that returns a "reference to int"
bar() is a function with a reference parameter, which is a "reference to int"
MyClass is a class with a member which is reference to int
funcX() is a function that returns a (non-reference type) int and xFunc() is an alias for funcX
const int& ref is a constant reference pointing to a piece of storage having value 65.
Types which are of kind "reference to <Type>" are sometimes called reference types. Identifiers which are of reference type are called reference variables. To call them variable, however, is in fact a misnomer, as we will see.
C++ references differ from pointers in several essential ways:
There is a simple conversion between pointers and references: the address-of operator (&) will yield a pointer referring to the same object when applied to a reference, and a reference which is initialized from the dereference (*) of a pointer value will refer to the same object as that pointer, where this is possible without invoking undefined behavior. This equivalence is a reflection of the typical implementation, which effectively compiles references into pointers which are implicitly dereferenced at each use. Though that is usually the case, the C++ Standard does not force compilers to implement references using pointers.