Const keyword in C++

Const keyword

On this page we will discuss about const keyword 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 Keyword

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 –

Run
#include <iostream>
using namespace std;
int 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:

int 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:

int 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
}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription