Close
Close

Const Keyword in C++


Const keyword is used to make any element of a program constant. Let's see its significance with different program elements.

Const Variables


If we declare a variable as const, we cannot change its value. A const variable must be assigned a value at the time of its declaration.

Once initialized, if we try to change its value, then we will get compilation error. The following two cases will give us an error since in both these cases, we are trying to change the value of a const variable.

int main()
{
    const int a = 5;          // declaring and initializing a const variable
    a = 10;          // this will give compilation error
    return 0;
}
int main()
{
    const int a = 5;          // declaring and initializing a const variable
    a++;          // this will give compilation error
    return 0;
}

Const Function Parameters and Return type


We can also make the type of argument and return type of a function const. Once declared constant, we will get compilation error if we try to change it.

void func( const int a )    // const argument
{
    a = 5;    // compilation error
}

This will result in compilation error since we cannot assign a value to any const variable after its declaration. Initialization of any const variable (in our case, a) takes place at the time of its declaration.

const int func()   // const return type
{
	return 4;
}

int main()
{
	int a;
	a = func();
	return 0;
}

In the above code, the return type of the function func is const and so we returned a const integer value.

Const Pointer


If we make a pointer const, we cannot change the pointer. This means that the pointer will always point to the same address but we can change the value of that address.

We declare a const pointer as follows.

int a = 4;
int* const p = &a   // const pointer p pointing to the variable a

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

Pointer to Const Variable


On the contrary to the above case, we can make the variable to which a pointer is pointing as const. Let's see how to declare a pointer to const variable.

const int* p;   // const pointer p pointing to a const int variable

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 Data Members of Class


Data members are just variables declared inside a class. const data members are not assigned values during its declaration. Const data members are assigned values in the constructor.

#include <iostream>

using namespace std;

class A
{
	const int x;
	public:
		A(int y)
		{
			x = y;
		}
};

int main()
{
	A a(5);
	return 0;
}

Constant Objects of Classes


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

const A a(4);

We made the object a of class A 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.

We cannot modify the data members of a const object.
#include <iostream>

using namespace std;

class A
{
	public:
		int x;
		A()
		{
			x = 0;
		}
};

int main()
{
	const A a;      // declaring const object 'a' of class 'A'
	a.x = 10;       // compilation error
	return 0;
}

The above program will give compilation error. Since we declared 'a' as a const object of class A, we cannot change its data members. When we created 'a', 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 compilation error.

Constant Member Function of Class


A const member function cannot change the value of any data member of the class and cannot call any member function which is not constant.

To make any member function const, we add the const keyword after the list of the parameters after the function name.

class A
{
	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 A 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

The reason for this is that 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.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Ask Yours
Post Yours
Doubt? Ask question