References in C++
When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration.
x = 20 ref = 30
- Modify the passed parameters in a function: If a function receives a reference to a variable, it can modify the value of the variable. For example, the following program variables are swapped using references.
- Avoiding a copy of large structures: Imagine a function that has to receive a large object. If we pass it without reference, a new copy of it is created which causes wastage of CPU time and memory. We can use references to avoid this.
- In For Each Loops to modify all objects : We can use references in for each loops to modify all elements.
- For Each Loop to avoid the copy of objects: We can use references in each loop to avoid a copy of individual objects when objects are large.
References vs Pointers:
Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. Despite the above similarities, there are the following differences between references and pointers.
1. A pointer can be declared as void but a reference can never be void For example
int a = 10; void* aa = &a; // it is valid void& ar = a; // it is not valid
2. The pointer variable has n-levels/multiple levels of indirection i.e. single-pointer, double-pointer, triple-pointer. Whereas, the reference variable has only one/single level of indirection. The following code reveals the mentioned points:
3. Reference variable cannot be updated.
4. Reference variable is an internal pointer .
5. Declaration of Reference variable is preceded with ‘&’ symbol ( but do not read it as “address of”).
- References are less powerful than pointers
1) Once a reference is created, it cannot be later made to reference another object; it cannot be reset. This is often done with pointers.
2) References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
3) A reference must be initialized when declared. There is no such restriction with pointers.
Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. In Java, references don’t have the above restrictions and can be used to implement all data structures. References being more powerful in Java is the main reason Java doesn’t need pointers.
- References are safer and easier to use:
1) Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don’t refer to a valid location (See questions 5 and 6 in the below exercise)
2) Easier to use: References don’t need a dereferencing operator to access the value. They can be used like normal variables. ‘&’ operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator (‘.’), unlike pointers where arrow operator (->) is needed to access members.
- Together with the above reasons, there are few places like the copy constructor argument where pointer cannot be used. Reference must be used to pass the argument in the copy constructor. Similarly, references must be used for overloading some operators like ++.
Predict the output of the following programs. If there are compilation errors, then fix them.
Related Articles :
- Pointers vs References in C++
- When do we pass arguments by reference or pointer?
- Can references refer to invalid location in C++?
- Passing by pointer Vs Passing by Reference in C++
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above