techmore.in

C++ References

In C++, references are a type of variable that act as an alias for another variable. They allow you to create a new name for an existing variable, providing a way to access or modify the original variable directly. Unlike pointers, references must be initialized when they are declared and cannot be changed to refer to another variable later.

1. Declaring and Initializing References

A reference is declared using the & symbol. Once a reference is initialized, it cannot be changed to refer to a different variable.

Syntax:

cpp
int x = 10; int& ref = x; // ref is a reference to x

Here, ref is a reference to x. Any changes made to ref will directly affect x, and vice versa.

2. Using References

Since references act as aliases, you can use them to access or modify the original variable they refer to.

Example:

cpp
#include using namespace std; int main() { int var = 10; int& ref = var; // ref is a reference to var cout << "Value of var: " << var << endl; // Outputs: 10 cout << "Value of ref: " << ref << endl; // Outputs: 10 ref = 20; // Modify the value through the reference cout << "Value of var after modifying ref: " << var << endl; // Outputs: 20 cout << "Value of ref after modification: " << ref << endl; // Outputs: 20 return 0; }

3. References as Function Parameters

References are commonly used in function parameters to avoid copying large objects and to allow functions to modify the arguments.

Example: Passing by Reference

cpp
#include using namespace std; void increment(int& num) { num++; // Modify the argument directly } int main() { int value = 5; increment(value); // Pass by reference cout << "Value after incrementing: " << value << endl; // Outputs: 6 return 0; }

4. References to Constants

You can also create references to constant values, which means you can read the value but cannot modify it.

Example:

cpp
#include using namespace std; void printValue(const int& num) { cout << "Value: " << num << endl; // num++; // Error: cannot modify a const reference } int main() { int value = 10; printValue(value); // Pass by const reference return 0; }

5. References and Arrays

References to arrays work similarly to pointers, but with some differences in syntax and behavior.

Example:

cpp
#include using namespace std; void printArray(int (&arr)[3]) { for (int i = 0; i < 3; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int arr[3] = {1, 2, 3}; printArray(arr); // Pass array by reference return 0; }

6. Reference Variables and Aliases

References provide an alternative name for a variable, and any operation on the reference affects the original variable.

Example:

cpp
#include using namespace std; int main() { int a = 5; int& b = a; // b is an alias for a cout << "a: " << a << endl; // Outputs: 5 cout << "b: " << b << endl; // Outputs: 5 b = 10; // Modify the value through the reference cout << "a after modifying b: " << a << endl; // Outputs: 10 cout << "b after modification: " << b << endl; // Outputs: 10 return 0; }

7. Reference to a Pointer

You can create a reference to a pointer, which can be useful for modifying the pointer itself or the object it points to.

Example:

cpp
#include using namespace std; void modifyPointer(int*& ptr) { int temp = 20; ptr = &temp; // Modify the pointer } int main() { int value = 10; int* p = &value; modifyPointer(p); cout << "Value pointed to by p: " << *p << endl; // Outputs: 20 return 0; }

8. Differences Between References and Pointers

  • Initialization: References must be initialized when declared, while pointers can be declared and initialized later.
  • Nullability: References cannot be null, whereas pointers can be.
  • Reassignment: References cannot be reassigned to refer to another variable, while pointers can.

Summary:

  1. References are aliases for other variables and must be initialized upon declaration.
  2. They provide a way to access and modify the original variable they refer to.
  3. Function parameters can be passed by reference to avoid copying and to allow modification.
  4. References to constants allow read-only access to values.
  5. References to arrays pass arrays efficiently.
  6. Reference variables act as aliases and any changes affect the original variable.
  7. References to pointers allow modification of the pointer itself or the data it points to.

References provide a more straightforward way to work with variables compared to pointers and can be very useful for managing function arguments and modifying data without unnecessary copying.