Type Qualifiers in C++

Type Qualifiers in C++

What are type qualifiers in C++?

C++ provides us the feature to add extra quality to our variables by specifying volatility or constantness to them. These extra quality are added with the help of tokens known as type qualifiers. In general words we can say that type qualifiers are the tokens that add special qualitties to the variables. These qualities specifies where the variables is to be stored in a memory and how it can be accessed from there. C++ programming language provides us three types of type qualifiers:-

  • const
  • volatile
  • mutable

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

Types of type qualifiers in C++

  • const
  • volatile
  • mutable

As already mentioned there are three types of type qualifiers in C++ , and each one of them is used to express additional information about a value.For example, if we want to represent a constant integer then we can type qualified that integer variable as const. 

 

Types of type qualifiers

1.) const

What is is const type qualifier in C++

In C++ const type qualifier is used to define that the type of the variable is constant.An object whose type is const type qualifier is a const object. If the type of object is cont then this type of object cannot be modified and if it we try to modify const type of object then there will be a compile time error.

2.) volatile

What is volatile type qualifier in C++

Volatile type qualifier in C++ defines that that the type of the variable is volatile. An object whose type is volatile qualifier is volatile object. If the object is volatile type qualified then it tells the compiler that a variable value can be changed in ways not explicitly specified by the program.

3.) mutable

What is mutable type qualifier in C++?

Mutable type qualifier is applied on non-static class members of non-reference and non-const type. And mutable members of const class instances are modifiable.This means that the member does not affect the external visible state of the class.

Syntax to define type qualified variables in C++

  • This is used to define that the type is constant.

Syntax

 const data_type variable = value;

Example

const int p = 1;
  • This is used to define that the type is volatile.

Syntax:

volatile data_type variable = value;

Example:

volatile int p = 1;
  • modifies variables that are const type qualified.

Syntax:

mutable data_type variable = value;

Example:

mutable int p = 1;

C++ program for type qualifiers

#include <iostream>
using namespace std;
int main()
{
    int num = 0;              // non-const object
    const int num1 = 0;      // const object
    volatile int num2 = 0;  // volatile object
    const struct
    {
        int num;
        mutable int num1;
    } 
    x = {0, 0};  //const object with mutable member
 
    num = 1;   // can be changed
    //num1 = 2;  // error,const type qualified variable
    num2 = 2 ;  
    const int& p2 = num1;  //const type variable modified.
    cout<<"num= "<<num<<endl<<"num1= "<<num1<<endl<<"num2= "<<num2;
}
Output:
num= 1
num1= 0
num2= 2