References in C++ (original) (raw)

References in C++ provide a way to create an alternative name for an existing variable. They allow programmers to access and modify the original variable directly without creating a separate copy, making programs more efficient and easier to read.

#include using namespace std;

int main() { int x = 10;

// ref is a reference to x.
int& ref = x;

// printing value using ref
cout << ref << endl;

// Changing the value and printing again
ref = 22;
cout << ref;

return 0;

}

`

**Explanation: In this program, ref is a reference to the variable x, meaning ref is just another name for x. When the value of ref is modified, it directly changes the value of x, since both ref and x refer to the same memory location.

If you've worked with pointers before, references may seem similar because both refer to the same memory location. However, references provide a simpler and more readable way to access an existing variable without using *.

Syntax

The ****&** symbol is used to declare a reference.

T ****&ref** = var;

**Parameters

Here, ref becomes an alias for the variable var.

**Important Points About References

Applications of References

References are widely used in C++ to avoid unnecessary copying and to provide direct access to existing objects. Some common applications are discussed below.

Passing Arguments by Reference

References are commonly used in function arguments to allow modification of the original variable passed to the function. They are also more efficient for large data structures since no copies are made.

C++ `

#include using namespace std;

void modifyValue(int &x) {

// Modifies the original variable
x = 20;  

}

int main() { int a = 10;

// Pass a by reference
modifyValue(a);

cout << a;
return 0;

}

`

**Explanation: The parameter **x is a reference to **a. Therefore, any modification made to **x inside modifyValue() directly changes the value of **a. No additional copy of the variable is created.

Returning Reference from Functions

Functions in C++ can return references to variables. This allows the returned value to be accessed or modified directly without creating a copy, making it useful for improving performance and enabling direct updates to existing objects.

C++ `

#include using namespace std;

int& getMax(int &a, int &b) {

// Return the larger of the two numbers
return (a > b) ? a : b;  

}

int main() { int x = 10, y = 20; int &maxVal = getMax(x, y);

// Modify the value of the larger number
maxVal = 30;  
cout << "x = " << x << ", y = " << y;
return 0;

}

`

**Explanation: The function **getMax() returns a reference to the larger of the two variables. Since **maxVal is a reference, assigning **30 to it directly modifies the original variable (y in this case). The output becomes x = 10, y = 30.

**Note: Never return a reference to a local variable because local variables are destroyed when the function returns, leaving the reference dangling.

**Modify Data in Range Based Loops

Range-based for loops normally iterate over elements by value. By using references, you can modify the original elements stored in the container directly.

C++ `

#include #include using namespace std;

int main() { vector vect{ 10, 20, 30, 40 };

// We can modify elements if we
// use reference
for (int& x : vect) {
    x = x + 5;
}

// Printing elements
for (int x : vect) {
    cout << x << " ";
}
return 0;

}

`

**Explanation: The loop variable x is declared as a reference (int&). Therefore, any modification made to x directly updates the corresponding element in the vector. Each element is increased by 5.

Limitations of References

Although references are convenient and safe to use, they have some limitations:

Advantages of References

References provide several benefits over pointers in many situations:

For a detailed comparison between references and pointers, refer to Pointers vs References.