Type Qualifiers in C++

What are type Qualifiers in C++?

C++ provides us with the feature to add extra quality to our variables by specifying volatility or constantness to them. C++ programming language provides us three types of type qualifiers:-

  • const
  • volatile
  • mutable

Further in this article, we will learn more about each type of qualifier in C++.

Type qualifier in c++

What is const type qualifier in C++?

  • If the type of object is cont then this type of object cannot be modified
  • If we try to modify the const type of object then there will be a compile-time error.
  • An object whose type is const type qualifier is a const object.

Syntax:

 const data_type variable = value;

Example

Run
#include <iostream>
using namespace std;
 
int main()
{
    const int var = 10;
    cout << var;
    
    var = 20; // error: Const values can't be changed
 
    return 0;
}

What is a volatile type qualifier in C++?

Saying that a variable is volatile would mean for us to tell the compiler to not optimize the code as the value for something may change.

To understand this we will need to take the below scenario –

int var = 123;

while(var == 123)
{
   //your code
}

We can do below that is just add a volatile keyword and the compiler will not optimize the code and knows the code/variables etc maybe sensitive that is volatile

volatile int var = 123;

while(var == 123)
{
   //your code
}

What is a mutable type qualifier in C++?

Const objects do not allow data member values to be changed. They can only be read after initialization and not updated again. Data members which are mutable are allowed to change their values even if they are of a const object. This may be needed when almost all other data members are required to remain constant but one may change. It’s a good idea to declare that data member as mutable. The following example demonstrates the same –
Run
#include<iostream>
using namespace std;

class MyClass {
    public:
        int var1;
        // mutable will allow var2 to be updated
        // even though its called by a const object
        mutable int var2;
        
    MyClass(int x=0, int y=0) {
        var1 = x;
        var2 = y;
   }
   void display() 
   {
        cout << endl << "var1: " << var1 << " var2: "<< var2 << endl;
   }
};
int main() 
{
    // const object, we can only read but not change values
    // values can only be initialized once, here we using a
    // parameterized constructor to initialize them
    const MyClass obj(10,20);
    
    // reading values is allowed as we are not changing
    // data members of const object
    cout << obj.var1 << " " << obj.var2 << "\n";
    
    // Below gives error as var1 can't be changed( because object is constant)
    // obj.var1 = 30;
    
    // var2 can be changed as var2 is mutable data member
    obj.var2 = 100;
    
    cout << obj.var1 << " " << obj.var2 << "\n";
    
    return 0;
}

Output

10 20
10 100

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