How to Use const in C++? A Beginner-Friendly Guide

Filed Under: C++
C Const Keyword

Hey, folks! In this tutorial, we will be focusing on const in C++.


Basics of const in C++

C++ has a huge set of keywords that help us give special functionality to the data variables on a whole.

const in C++ makes a particular data entity, a constant one i.e. during the scope of the entire program, the value of the const variable remains the same.

In the actual scenarios, the const keyword is very essential to lock a particular value as static and constant throughout its use.

Let us take an example.

For an automatic emailing system, wherein the owner of a company wants to send a mail at the last date of every month to his employees, the email address of the owner would stay the same. So we can declare a variable to save the owner’s email address and make it constant using the const keyword.

Thus, we would actually save time and reduce the complexity of the system.


Variants of const in C++

The const keyword in C++ can be used alongside different data entities of programming such as:

  • Data variables
  • Function arguments
  • Pointers
  • Objects of a class
  • Class data members

1. Data variables as const

When a data variable is declared as const, the value equated to the variable is treated as constant and is immutable.

Syntax:

const data-type variable = value;

Example:

#include<iostream>
using namespace std;
int main()
{
    const int A=10,B=20;
    cout<<"Addition: "<<(A+B);
  
}

Output:

Addition: 30

Error and Exceptions:

If we try to change or manipulate the value of a const data variable, the compiler would throw an error saying that we cannot assign values to a read-only variable.

#include<iostream>
using namespace std;
int main()
{
    const int A=10;
    A+=10;
    cout<<A;
  
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:6:8: error: assignment of read-only variable ‘A’
     A+=10;

2. Function arguments as const in C++

We can even assign an argument to a function/method as constant, thus restricting all the changes to the function argument.

Syntax:

data-type function(const data-type variable)
{
  //body
}

Example:

#include<iostream>
using namespace std;

int add(const int x)
{   
    int res=0;
    int error=20;
    res = x+error;
    return res;
}
int main()
{
  int ans = add(50);
  cout<<"Addition:"<<ans;
}

Output:

Addition:70

Error and Exceptions:

As seen below, if we try to manipulate the value of a const function argument, an error would be raised by the compiler.

#include<iostream>
using namespace std;

int add(const int x)
{   x=x+100;
    return x;
}
int main()
{
  int ans = add(50);
  cout<<"Addition:"<<ans;
}

In the above example, we are trying to change the value of the const function argument “x”, resulting in a compiler error.

Output:

main.cpp: In function ‘int add(int)’:
main.cpp:5:9: error: assignment of read-only parameter ‘x’
 {   x=x+100;

3. Pointer as const

Pointer variables can be declared as constant too using the below syntax:

data-type const *variabl;

Example:

#include<iostream>
using namespace std;
int main()
{   int age = 21;
    int const *point = & age;
    cout<<"Pointer value: "<<*point;
  
}

Output:

Pointer value: 21

Error and Exceptions:

If we try to manipulate the value of a const pointer, that leads to a compilation error.

#include<iostream>
using namespace std;
int main()
{   int age = 21;
    int const *point = & age;
    *point=*point+10;
    cout<<"Pointer value: "<<*point;
  
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:6:19: error: assignment of read-only location ‘* point’
     *point=*point+10;
                   

4. Data variables of a class as const

The Class variables can also be treated as const to make the value of the data variable as constant and immutable.

Syntax:

const data-type variable = value;

Example:

#include<iostream>
using namespace std;
class Demo
{
    public:
    const int X = 10;
    
};
int main()
{
   Demo d;
   cout<<d.X;
  
}

Output:

10

Error and Exceptions:

#include<iostream>
using namespace std;
class Demo
{
    public:
   const int X = 10;
    
};
int main()
{
   Demo d;
   d.X=100;
   cout<<d.X;
  
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:12:8: error: assignment of read-only member ‘Demo::X’
    d.X=100;

5. Object of a class as const

Even the objects of a class can work with the const keyword. As soon as we declare the object of a class as constant, the data variables of the same becomes immutable i.e. the values of the data variables that belongs to the class pointed by the object cannot be changed.

Syntax:

const Class object;

Example:

#include<iostream>
using namespace std;
class Demo
{
    public:
    int X = 10;

};
int main()
{
   const Demo d;
   cout<<d.X;
  
}

Output:

10

Error and Exceptions:

#include<iostream>
using namespace std;
class Demo
{
    public:
    int X = 10;

};
int main()
{
   const Demo d;
   d.X=100;
   cout<<d.X;
  
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:12:8: error: assignment of member ‘Demo::X’ in read-only object
    d.X=100;

Conclusion

Thus, in this article, we have unleashed the working of const keyword alongside data variables, pointers, Class objects, etc.


References

C++ const keyword — Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages