Reference Variable in C++
Reference Variables. C++ added the so-called reference variables (or references in short). A reference is an alias or an alternate name to an existing variable. For example, suppose you make peter a reference (alias) to paul, you can refer to the person as either peter or paul.
A reference variable is a variable that points to an object of a given class, letting you access the value of an object. For example, you can retrieve a row from a database table and assign all values from the row to a single object and then pass the object to a called procedure.
References are often confused with pointers but three major differences between references and pointers are:
- You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
- Once a reference is initialized to an object, it cannot be changed to refer to another object.
- Pointers can be pointed to another object at any time.
- A reference must be initialized when it is created. Pointers can be initialized at any time.