Top Level and Low Level Const in C++



Top Level and Low Level Consts in C++


Let's look at an example..

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
  using namespace std;

  int main() {
  	  int int1 = 1, int2 = 2;       // Defining two integer variables
	  int* const ptr1 = &int1;      // ptr1 has a top - level const
	  // ptr1 is a constant pointer to integer variable int1

	  ptr1 = &int2;                 // error : we cannot assign the address of any other variable to ptr1
	  *ptr1 = 3;                    // we can change the value of int1 through ptr1
	  const int* ptr2 = &int1;      // ptr2 has a low level const
	  // ptr2 is a pointer to a constant integer

	  *ptr2 = 3;                    // error : we cannot change the value of int1 through ptr2
	  ptr2 = &int2;                 // we can assign address of another variable to ptr2, now ptr2 points to int2
	  const int* const ptr3 = &int1;  // here ptr3 has both top level and low level const

	  const int int3 = 3;	        // Defining a constant integer variable int3, here int3 has a top level const
	  const int* ptr4 = &int3;	// ptr4 is a pointer to a constant integer, here ptr4 has a low level const

	  return 0;
  }

In the above example, on line 5 we defined two integer variables 


int int1 = 1, int2 = 2;         // Defining two integer variables

on line 6, we define ptr1 which is a constant pointer to an integer and it is assigned the address of int1.


int* const ptr1 = &int1;        // ptr1 has a top - level const

Here ptr1 has a top level const, that means we cannot assign an address of some other variable to it.

On line 9 we try to assign the address of int2 to ptr1,

 ptr1 = &int2;  // error : we cannot assign the address of any other variable to ptr1

which is an error because ptr1 is a constant pointer to int1. Although we can change the value of int1 through ptr1.

*ptr1 = 3;     // we can change the value of int1 through ptr1

On line 11. we define ptr2, which is a pointer to a constant integer and is also assigned the address of int1. Here ptr2 has a low level const. Although int1 is not a constant, the definition means that we cannot change the value of int1 through ptr2.

const int* ptr2 = &int1;    // ptr2 has a low level const
// ptr2 is a pointer to a constant integer

On line 14, we try to change the value of int1 through ptr2,

*ptr2 = 3;   // error : we cannot change the value of int1 through ptr2

that's an error because ptr2 is a pointer to constant integer. we can assign the address of another variable to ptr2.

ptr2 = &int2;      // we can assign address of another variable to ptr2,
                   // now ptr2 points to int2

On line 16, we define ptr3, which is a constant pointer to constant integer.

const int* const ptr3 = &int1;  // here ptr3 has both top level and low level const

Here, ptr3 which is also assigned the address of int1, has both top level and low level const, that means neither we can assign a different address to ptr3, nor we can change the value of the variable ptr3 is pointing to.

Now, at line 18 we defined a constant int named int3.


const int int3 = 3;	// Defining a constant integer variable int3, here int3 has a top level const

int3 is a constant, so it's value cannot be changed.

On line 19, we defined a pointer to a constant integer named ptr4, it is pointing towards a constant integer, that's why is has a low level const, otherwise it would have been an error.

Comments