### Const Keyword in C++

**Notion of const-ness**

1. The value of const variable cannot be changed after definition.

const int n=10 ; //n is a int type variable with value 10 and n is constant n=5; //compilation error as n cannot be changed int m; int *p=0; p=&m; //Hold m by pointer p *p=7; //change m by p and m is now 7 p=&n; //compilation error as n may be changed by *p=5;

2. Naturally, a const variable must be initialized when defined.

typedef struct _Complex{ double re; double im; }complex; const complex c={2.3,7.5}; /*c is a complex type variable. It is initialize with c.re=2.3 and c.im=7.5 and c is contant */ c.re = 3.5; //Is a compilation error as no part of c can be changed

### Compare between #define and const

#### Using #define

#include<iostream> #include<cmath> using namespace std; #define TWO 2 #define PI 4.0*atan(1.0) int main(){ int r=10; double peri=TWO*PI*r; cout<<”Perimeter = “<<peri<<endl; return 0; }

Output : Perimeter = 314.159

- TWO is a manifest constant
- PI is a manifest constant
- TWO and PI look like variables
- Types of TWO and PI may be indeterminate

#### Using const

#include<iostream> #include<cmath> using namespace std; const int TWO=2; const double PI 4.0*atan(1.0); int main(){ int r=10; double peri=TWO*PI*r; //no replacement by cpp cout<<”Perimeter = “<<peri<<endl; return 0; }

Output : Perimeter = 314.159

- TWO is a const variable initialized to 2
- PI is a const variable initialized to 4.0*atan(1.0)
- TWO and PI are variables
- Type of TWO is const int
- Type of PI is const double

### Advantages of const

Naturally constants like ,e,(Golden Ratio) etc. can be compactly defined and used like

const int True =1;

const int False=0;

const NULL=0;

Note : NULL is a manifest constant in C/C++ set to 0.

Program constants like number of elements, arrays size etc. can be defined at one place (at times in a header ) and used all over the program.

### Prefer const over #define.

#### Using #define

- Is not type safe.
- Replaced textually by cpp.
- Cannot be watched in debugger.
- Evaluated as many times as replaced.

#### Using const

- Has its type.
- Visible to the compiler.
- Can be watched in debugger.
- Evaluated only on initialization.

### const and pointers

const-ness can be used with pointers in one of the two ways:

- Pointer to constant data where the pointee (pointed data) cannot be changed.
- Constant pointer where the pointer(address) cannot be changed.

Consider usually pointer-pointee computation (without const) :

int m=4; int n=5; int *p=&n; //p points to n and *p=5 n=6; //n and *p is 6 now *p=7; //n and *p is 7 now and pointee changes p=&m; //p points to m , *p is 4 and pointer changes *p=8; //m and *p is 8 now, n is 7 and pointee change Consider pointed data int m=4; const int n=5; const int *p=&n; n=6; //Error : n is constant and cannot be changed *p=7; //Error: p points to a constant data (n) that cannot be changed p=&m; //Okay *p=8; //Okay

Interestingly :

int n=5; const int *p=&n; n=6; //Okay *p=6; //Error : p points to a “constant” data (n) that cannot be changed

CommentsNo comment yet.