« Constant variables
Understand constant variables
Constant variable
1//int variable2int i = 10;3i = 12;45//constant variable6const int j = 10;7int const y = 10;8// we can write const int or int const910// j = 12; //error11// 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++; // error4m++;
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 reference from const int
1const int j2 = 10;2const int &k2 = j2;3// j2++; // error4// 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++;//error4}5// This gives the contract that this function won't change the value at the storage678void f(const int *p)9{10 //(*p)++; //error11}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 int56//(*pe)++;//error7pe = &d;8910int *const pd = &d; // pd is a const pointer to a integer.11(*pd)++;12cout << "d: " << d << endl;13// pd = &e; //error141516int const *const p3 = &e; // p3 is a const pointer to a const integer17//(*p3)++; //error18// 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_MIN11#include <unordered_map>12using namespace std;1314void g(const int &a)15{16 // a++;//error17}1819void f(const int *p)20{21 //(*p)++; //error22}2324int main()25{26 int i = 10;27 i = 12;2829 const int j = 10;30 int const y = 10;31 // j = 12; //error3233 // const int k; //error.34 // k = 30;3536 int m = 10;37 const int &x = m; // constant reference from a non const integer3839 // x++; // error40 cout << "x: " << x << endl;41 m++;42 cout << "x: " << x << endl;4344 // const reference from const int45 const int j2 = 10;46 const int &k2 = j2;47 // j2++; // error48 // k2++; // error4950 // reference from const int51 int const j3 = 123;52 // int &k3 = j3; //error5354 // Constant pointers55 const int n = 10;56 // int *pn = &n; // error5758 const int *pn = &n; // constant pointer from a int.5960 //(*pn)++; // error61 int q = 20;62 int const *pq = &q;63 cout << "*pq: " << (*pq) << endl;64 q++;65 cout << "*pq: " << (*pq) << endl;6667 int e = 10;68 int d = 20;69 int const *pe = &e; // pe is a pointer to a constant int7071 //(*pe)++;//error72 pe = &d;73 int *const pd = &d; // pd is a const pointer to a integer.7475 (*pd)++;76 cout << "d: " << d << endl;77 // pd = &e; //error78 int const *const p3 = &e; // p3 is a const pointer to a const integer7980 //(*p3)++;81 // p3 = &d;82 return 0;83}