Different ways to use Const with Reference to a Pointer in C++
Before moving forward with using const with Reference to a Pointers, let us first see what they are one by one:
- Pointers are used to store the address of variables or a memory location. A variable can be declared as a pointer by putting ‘*’ in the declaration.
datatype *var_name;
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int * ptr_i = &i;
cout << *ptr_i;
return 0;
}
|
- Reference: 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.
datatype &var_name;
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int & ref = i;
ref = 20;
cout << i;
return 0;
}
|
- References to pointers is a modifiable value that’s used same as a normal pointer.
datatype *&var_name;
Example 1:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int * ptr_i = &i;
int *& ptr_ref = ptr_i;
cout << *ptr_ref;
return 0;
}
|
Here ptr_ref is a reference to the pointer ptr_i which points to variable ‘i’. Thus printing value at ptr_ref gives the value of ‘i’, which is 10.
Example 2: Now let us try to change the address represented by a Reference to a Pointer
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int j = 5;
int * ptr = &i;
int *& ptr_ref = ptr;
ptr_ref = &j;
cout << *ptr;
return 0;
}
|
Here it prints 5, because the value of j is 5 and we changed ptr_ref to point to j. Now as ptr_ref is a reference to pointer ptr, ptr now points to j. Thus we get the output we expected to see.
- Const Reference to a pointer is a non-modifiable value that’s used same as a const pointer.
datatype* const &var_name;
Example 1:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int j = 5;
int * ptr = &i;
int * const & ptr_ref = ptr;
ptr_ref = &j;
cout << *ptr;
return 0;
}
|
Compilation Error:
In function 'int main()':
prog.cpp:23:13: error: assignment of read-only reference 'ptr_ref'
ptr_ref = &j;
^
Here we get a compile-time error as it is a const reference to a pointer thus we are not allowed to reassign it.
Example 2:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int j = 5;
int * ptr = &i;
int * const & ptr_ref = ptr;
*ptr_ref = 100;
cout << *ptr;
return 0;
}
|
It prints 100 as it is not a reference to a pointer of a const.
Why Example 2 didn’t throw a Compile-time error when Example 1 did?
- In Example 1, the ptr_ref is a const reference to a pointer to int, and we are trying to change the value of ptr_ref. So the compiler throws a Compile time error, as we are trying to modify a constant value.
- In Example 2, the ptr_ref is a const reference to a pointer to int, and we are trying to change the value of *ptr_ref, which means we are changing the value of int to which the pointer is pointing, and not the const reference of a pointer. So the compiler doesn’t throw any error and the pointer now points to a value 100. Therefore the int is not a constant here, but the pointer is. As a result, the value of int changed to 100.
- Reference to a Const Pointer is a reference to a constant pointer.
datatype const *&var_name;
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int j = 5;
int const * ptr = &i;
int const *& ptr_ref = ptr;
*ptr_ref = 124;
cout << *ptr;
return 0;
}
|
Compilation Error:
In function 'int main()':
prog.cpp:23:14: error: assignment of read-only location '* ptr_ref'
*ptr_ref = 124;
^
Here again we get compile time error. This is because here the compiler says to declare ptr_ref as reference to pointer to const int. So we are not allowed to change the value of i.
Last Updated :
04 Apr, 2020
Like Article
Save Article