
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the const Keyword in C++
The const keyword in C++ is a keyword that is used to declare variables and objects as constant, which means the value declared using const cannot be changed or modified later, once they are initialized. This helps them prevent accidental modifications.
For example, in a code, if we are using the value of PI, which has a fixed universal value and doesn't need any change, then we can declare it as a constant.
When you declare the object with the const keyword, then the compiler places that value in ROM (Read-Only Memory), which protects it from being changed in the future. And if we try to reassign that const value, then it will return an error.
Syntax
Here is the following syntax for the const keyword.
const data_type variable_name = value;
Example
#include <iostream> #include <string> using namespace std; class constObject { public: void display() const { // object cout << "Here displaying the Const Object" << endl; } }; int main() { const int x = 10; // const variable const string msg = "Hello"; // const string const int arr[] = {1, 2, 3}; // const array int val = 100; const int* ptr = &val; // pointer to const int const constObject obj; // const object obj.display(); // only const member functions allowed cout<<"const variable: "<<x<<endl; cout<<"const string: "<<msg<<endl; cout<<"const array calling index 1: "<<arr[1]<<endl; cout<<"pointer to const int: "<<*ptr<<endl; return 0; }
Output
Here displaying the Const Object const variable: 10 const string: Hello const array calling index 1: 2 pointer to const int: 100
Const Member Function
A const member function in C++ is a function that does not change or modify any data members of the class; this makes the function read only and can be called on const objects. This is also declared using the const keyword.
Syntax
Here is the following syntax for a const member function in C++.
class class_name { public: return_type function_name() const; };
Example
#include <iostream> using namespace std; class Student { int rollNumber; public: // Constructor initializing roll number Student(int r) : rollNumber(r) {} // Const member function void display() const { cout << "Roll Number: " << rollNumber << endl; } }; int main() { const Student s(23); // const object s.display(); return 0; }
Output
Roll Number: 23
Const vs. Constexpr
In C++, both const and constexpr create constants, but they are used for different purposes.
const declares a read-only variable, which means the value cannot be changed or modified later once initialized. This is evaluated at both run time and compile time.
Whereas constexpr defines a constant, which evaluates the value only at compile time, therefore making the value a compile time constant. For example, for array sizes, template parameters, etc.
Example
constexpr int x = 5;