The possible uses of the const
qualifier with C++ pointers:
- pointer to const variable
- const pointer to variable
- const pointer to const variable
All examples below will be using the same struct MyType
defined below:
1
2
3
4
5
6
7
8
9
struct MyType {
int _x;
MyType(int x): _x(x) {}
friend std::ostream& operator<<(std::ostream& out, const MyType &my_type)
{
out << my_type._x;
return out;
}
};
We declare two variables of type MyType
as follows:
1
2
MyType a{1};
MyType b{2};
Pointer to const variable
When using a pointer to a const
variable, we cannot change the value of the variable pointed to by the pointer. However, we can reassign the pointer itself:
1
2
3
4
5
6
// pointer to const variable
const MyType *ptr1 = &a;
std::cout << "ptr1: " << *ptr1 << std::endl;
// ptr1->_x = 33; // error
ptr1 = &b;
std::cout << "ptr1: " << *ptr1 << std::endl;
Output:
1
2
ptr1: 1
ptr1: 2
Const pointer to variable
When using a const
pointer to a variable, we can change the value of the variable pointed to by the pointer. However, we cannot reassign the pointer. It is the opposite of the situation above:
1
2
3
4
5
6
// const pointer to variable
MyType* const ptr2 = &a;
std::cout << "ptr2: " << *ptr2 << std::endl;
ptr2->_x = 10;
std::cout << "ptr2: " << *ptr2 << std::endl;
// ptr2 = &b; // error
Output:
1
2
ptr2: 1
ptr2: 10
Const pointer to const variable
When using a const
pointer to a const
variable, we can neither change the value of the variable pointed by the pointer, nor reassign the pointer:
1
2
3
4
5
// const pointer to const variable
const MyType* const ptr3 = &b;
std::cout << "ptr3: " << *ptr3 << std::endl;
// ptr3->_x = 33; // error
// ptr3 = &a; // error
Output:
1
ptr3: 2
Conclusion
Here is the recap in table form:
variable | const variable | |
pointer | can change value; can reassign pointer | cannot change value; can reassign pointer |
const pointer | can change value; cannot reassign pointer | cannot change value; cannot reassign pointer |
Comments powered by Disqus.