« Constant variables

Understand constant variables

Constant variable

1//int variable
2int i = 10;
3i = 12;
4
5//constant variable
6const int j = 10;
7int const y = 10;
8// we can write const int or int const
9
10// j = 12; //error
11// you can not change a const value
1// const int k;
2// k = 30; //error.

If we don't initialize a const variable while declaring it then it will always have a garbage value so C++ doesn't allow this. So we need to initialize constant variable while declaring it.

Const reference from a non const variable

1int m = 10;
2const int &x = m;
3// x++; // error
4m++;

Storage is never constant its just the path is constant. Here out of two paths x is constant but m is not, so we can change value at storage using m.

const_ref_non_const_variable.jpg

Const reference from const int

1const int j2 = 10;
2const int &k2 = j2;
3// j2++; // error
4// k2++; // error

Reference from const int

1int const j3 = 123;
2// int &k3 = j3; //error

j3 only has read access to its memory then how can k3 will ever get read and write access to the memory.

Pointer from a const variable

1const int n = 10;
2// int *pn = &n; // error//

You can't store address of a const integer into a normal pointer. You only have read access to the storage so you can't facilitate a write allowed path to the storage.

Const pointer from a const variable

1const int n = 10;
2const int *pn = &n;
3//(*pn)++; // error

pn is a pointer to a constant int.

Const pointer from a non const variable

1int q = 20;
2int const *pq = &q;
3q++;
4//(*pq)++; //error

Passing const pointers and references to functions

1void g(const int &a)
2{
3 // a++;//error
4}
5// This gives the contract that this function won't change the value at the storage
6
7
8void f(const int *p)
9{
10 //(*p)++; //error
11}
12// This gives the contract that this function won't change the value at the storage

We can use const at functions when we don't want functions to change the values

Interesting case of const pointers

1int e = 10;
2int d = 20;
3int const *pe = &e;
4// pe is a pointer to a constant int
5
6//(*pe)++;//error
7pe = &d;
8
9
10int *const pd = &d; // pd is a const pointer to a integer.
11(*pd)++;
12cout << "d: " << d << endl;
13// pd = &e; //error
14
15
16int const *const p3 = &e; // p3 is a const pointer to a const integer
17//(*p3)++; //error
18// p3 = &d; //error

pd is a const pointer to a integer. pd is constant and where pd is pointing thats not constant. p3 is a const pointer to a const integer.

Final code

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14void g(const int &a)
15{
16 // a++;//error
17}
18
19void f(const int *p)
20{
21 //(*p)++; //error
22}
23
24int main()
25{
26 int i = 10;
27 i = 12;
28
29 const int j = 10;
30 int const y = 10;
31 // j = 12; //error
32
33 // const int k; //error.
34 // k = 30;
35
36 int m = 10;
37 const int &x = m; // constant reference from a non const integer
38
39 // x++; // error
40 cout << "x: " << x << endl;
41 m++;
42 cout << "x: " << x << endl;
43
44 // const reference from const int
45 const int j2 = 10;
46 const int &k2 = j2;
47 // j2++; // error
48 // k2++; // error
49
50 // reference from const int
51 int const j3 = 123;
52 // int &k3 = j3; //error
53
54 // Constant pointers
55 const int n = 10;
56 // int *pn = &n; // error
57
58 const int *pn = &n; // constant pointer from a int.
59
60 //(*pn)++; // error
61 int q = 20;
62 int const *pq = &q;
63 cout << "*pq: " << (*pq) << endl;
64 q++;
65 cout << "*pq: " << (*pq) << endl;
66
67 int e = 10;
68 int d = 20;
69 int const *pe = &e; // pe is a pointer to a constant int
70
71 //(*pe)++;//error
72 pe = &d;
73 int *const pd = &d; // pd is a const pointer to a integer.
74
75 (*pd)++;
76 cout << "d: " << d << endl;
77 // pd = &e; //error
78 int const *const p3 = &e; // p3 is a const pointer to a const integer
79
80 //(*p3)++;
81 // p3 = &d;
82 return 0;
83}