Declaring and defining references in C++

To declare a reference in C++, you use the ampersand symbol (&) after the type. Here’s an example:

int num = 10;
int& ref = num;

In the above code, we declare a reference named ref of type int&, and initialize it with the variable num. Now, ref is an alias for num, and any changes made to ref will affect num and vice versa.

It’s important to note that a reference must be initialized when declared and cannot be changed to refer to a different variable. Therefore, the following code would result in a compilation error:

int& ref; // Error: references must be initialized

Once a reference is declared and defined, you can use it just like you would use the original variable. Here’s an example of using a reference to modify a value:

int num = 10;
int& ref = num;

ref = 20;
std::cout << num << std::endl; // Output: 20

In the code above, changing the value of ref to 20 actually changes the value of num since they both refer to the same memory location.

References are commonly used as function parameters to pass values by reference, allowing you to modify the original variable inside the function. They are also often used in range-based for loops to iterate over a collection of items without making a copy of each item.

Remember, references in C++ are powerful, but you should use them with caution to ensure they are properly initialized and used correctly to avoid any unintended consequences or errors in your code.

#C++ #References