Const keyword in C++

How Const Keyword works in C++

Generally, variables can be modified at any point of time in the program, but the address of the const members is locked after single initialization, restricting further modification.

Const in C++

Constant Variables

A variable declared as constant, will not allow any modifications after its initialization. Constant variables are created using the keyword const

For example –

#include <iostream>
using namespace std;

main()
{
int a1=10; //normal variable
const int a2=20; //const variable

//works fine as normal variable
a1++;

//this would give error as
//we are trying to change value in const variable

a2++;

return 0;
}

Constant Member Function of Class

  • A constmember function cannot change any data members of the class and it also cannot call any non-const function
  • It is a read-only function
  • To make any member function const, we add the const keyword
    after the list of the parameters after the function name.
class test
{
public:
int x;
void func() const
{
x = 0; // this will give compilation error
}
};
  • The above code will give us compilation error because ‘func()’ is a const member function of class test and we are trying to assign a value to its data member ‘x’.
  • A const object can only call a const member function, this is because a const object cannot change the value of the data members and a const member function also cannot change the value of the data member of its class. So, a const object would like to call a function which does not violate its rule.
  • We cannot make constructors const Generally, const objects initialize the values of their data members through constructors and if we make the constructor const, then the constructor would not change the values of the data members and the object would remain uninitialized.

Constant Pointer

  • If we make a pointer const, Its address cannot be changed after initialization
  • This means that the pointer will always point to the same address.
  • Here address of const pointer cant be changed but the value that is pointed can be changed

Example:

main()
{
int a = 4,b=5;
int* const ptr = &a; // const pointer p pointing to the variable a
//ptr=&b;//invalid :const pointer address cannot be changed
*ptr=6;//valid,value at ptrs address is non_const
}

Note that we cannot change the pointer p but can change the value of a.

Pointer to Constant Variable

Here, the value that is pointed by the pointer cannot be modified

Example:

main()
{
int a = 4,b=5;
const int *p=&a;//a becomes constable variable
//*p=5;//inavlid:a is const
p=&b;//valid: address pointed by p is non_const
}

Here, p is a pointer which is pointing to a const int variable, this means that we cannot change the value of that int variable.

Constant objects

  • We cannot modify the data members of a const object, only one initialization is allowed for all the copies of that object
  • const objects are reads only objects

We make an object const by writing the const keyword at the beginning of the object declaration as shown below.

const test t(5);
  • We made the object a of class test const by writing the const keyword before the class name at the time of its declaration.
  • A const class object is initialized through the constructor.

Example:

class test
{
public:
int x;
test()
{
x = 0;
}
};
main()
{
const test t; // declaring const object 'a' of class 'A'
// t.x = 10; // compilation error
}
  • The above program will give a compilation error.
  • Since we declared ‘t’ as a const object of the class test, we cannot change its data members.
  • When we created ‘t’, its constructor got called assigning a value 0 to its data member ‘x’. Since ‘x’ is a data member of a const object, we cannot change its value further in the program. So when we tried to change its value, we got a compilation error.

Constant Data Members

  • Once initialized, a const data member may never be modified, not even in the constructor or destructor.
  • The const data member must be initialized by the constructor using an initialization list.

Example:

class test
{
const int x;//const_data member
public:
test(int y)
{
x = y;
}
test(float z)
{
//x = z;//const memeber x cannot be reassigned
}
};
main()
{
A a(5);
//A b(2.3);//invalid :assignment of read_only_object
}