We all are familiar with an alias in C++. An alias means another name for some entity. So, a reference variable is an alias that is another name for an existing variable/object etc.
Below is the program for adding reference to a variable:
Value of a: 9 After Update: Value of a :100 Value of N :100
Explanation: In the above program, a variable a is an alias of variable N that means we have given another name to variable N. So what ever we are doing with a it will effect N also and vice-versa.
Therefore, when we change the value of a to 100, then, value of N also changed to be 100.
Reference of an object in Container Classes:
The above method is correct to give an alias to any variable but in the case of containers the above method will throw a Compilation Error because containers directly can’t store the references, But there is an alternative way of doing the same. The template std::reference_wrapper in C++ STL is used to give reference to any containers in C++. The std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers(like in vector, list, etc) which cannot normally hold references.
Below is the program for adding a reference of an object in container class:
Value of a for object obj is 5 After Update Value of a for object obj is 700 Value stored in the list is 700
In the above program, when an object is created of class gfg, the constructor is called and the value of variable a is initialized to 5. We have stored the reference of the object in the list and then we have changed the value of the variable a to 700 by calling the member function setValue(). Now, when we see the value of the property a of the object whose reference we had stored in the list. The value stored is 700.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Interval Tree using GNU Tree-based container
- When do we pass arguments by reference or pointer?
- Passing by pointer Vs Passing by Reference in C++
- Reference to a pointer in C++ with examples and applications
- Passing Reference to a Pointer in C++
- Difference between Call by Value and Call by Reference
- Can C++ reference member be declared without being initialized with declaration?
- Different ways to use Const with Reference to a Pointer in C++
- Why do we need reference variables if we have pointers
- Return by reference in C++ with Examples
- Virtual functions in derived classes
- Catching base and derived classes as exceptions
- Nested Classes in C++
- Local Classes in C++
- Pure Virtual Functions and Abstract Classes in C++
- C++ Classes and Objects
- Trivial classes in C++
- Anonymous classes in C++
- File Handling through C++ Classes
- Mutual friendship of Classes in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.