const Qualifier in C++

In C++, the const qualifier is used to define constants or variables that cannot be modified after they have been initialized. If we attempt to alter a const variable then it results in a compile-time error. This restriction helps in reducing bugs by preventing unintended modifications to variables and it also allows the compiler to optimize code assuming the values will remain unchanged.

Syntax of const in C++

 const dataType variableName = value ;

Example

The below example demonstrates how we can use const qualifier in C++.

C++
// C++ program to demonstrate the use of const qualifier

#include <iostream>
using namespace std;

int main()
{
    // define a const variable num
    const int num = 50;
    // print the value of num
    cout << "Value of num is: " << num;

    // The below line will throw an Error: because we are
    // Attempting to modify a const variable num = 50;
    //const  int num =45;
    // cout << "Value of num is: " << num;

    return 0;
}

Output
Value of num is: 50

Type Qualifiers in C++

In C++, type qualifiers are keywords that modify the properties of data types, influencing how variables, pointers, or references can be used. These qualifiers enhance variable declarations by providing additional information on their access and usage constraints, ensuring more controlled and secure code behavior. In C++ common type qualifiers include: const, volatile, and mutable, each serving to specify particular attributes or constraints for handling variables and memory.

Similar Reads

const Qualifier in C++

In C++, the const qualifier is used to define constants or variables that cannot be modified after they have been initialized. If we attempt to alter a const variable then it results in a compile-time error. This restriction helps in reducing bugs by preventing unintended modifications to variables and it also allows the compiler to optimize code assuming the values will remain unchanged....

volatile Qualifier in C++

In C++, the volatile qualifier is used to mark variables that might change unexpectedly, often due to external factors like hardware interactions. This keyword can be applied to variables, as well as to structures and unions to ensure that their member variables are treated as volatile. When we declare a variable as volatile, the compiler is instructed not to optimize code involving this variable to ensure that every access to the variable is directly to its actual memory location, preventing the compiler from caching the variable in registers or removing seemingly redundant accesses....

mutable Qualifier in C++

In C++, the mutable qualifier allows a class member to be modified even when the containing object is declared as const. This is particularly useful for members that do not contribute to the object’s externally visible state. For example, variables used for caching, lazy evaluation, or tracking state internally, where changes do not affect the object’s logical consistency. Unlike the const qualifier, which enforces immutability, mutable enables controlled mutability, allowing for modifications in otherwise immutable contexts....

Conclusion

In conclusion, type qualifiers are really useful in many cases. We can use const to make the variable immutable. volatile keyword can be used to indicate that the variable declared with the volatile keyword might change unexpectedly. Also, we have a mutable keyword that allows us flexibility as we can modify the class member even when the containing object is declared as const....

Contact Us