
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 are Type Qualifiers in C++
A type qualifier is a keyword in C++ that is applied to a variable, function, pointer, or parameter to add an extra feature or quality to it.
For example, const int is a qualified type representing a constant integer, while int is an unqualified type, which is simply just an integer. Type qualifiers are a way of expressing additional information about a value through the type system, which ensures correctness in the use of the data.
1. The const Qualifier
This is used to define a variable (or object) as constant, which means its value cannot be changed or modified further once initialized with the const qualifier.
Syntax
Here is the following syntax of declaring variable as constant using const qualifier.
const data_type variable_name = value;
Example code
In this following example we have used the const qualifier (or keyword) to declare PI variable. As value of PI is universal constant, so declaring it as const will prevent it from any accidental changes or modifications in the code.
#include <iostream> using namespace std; int main() { const float PI = 3.14159f; // constant value of pi float radius = 3; float area = PI * radius * radius; // as area is equal to ?r² cout << "Area of the circle is: " << area << endl; return 0; }
Output
Area of the circle is: 28.2743
2. The volatile Qualifier
This is used to mark the variables that might get changed unexpectedly because of any external factors like hardware, another thread, or an interrupt. This keyword is used with variables, structures, and unions to tell the compiler not to optimize this variable, because its value may change at any time unexpectedly.
Syntax
Here is the following synatx of volatile qualifier, which declares a variable whose value might get changed unexpecdily due to any external factors.
volatile data_type variable_name;
Example
Here is the following code, in which we declared flag variable as volatile because in case if we want to use flag variable as signal for communication between threads or hardware and software, so the shared signal value may get changed outside the current code. Therefore declaring it volatile will tell the complier not to optimize accesses to this variable and to always read its value directly from memory.
#include <iostream> using namespace std; //This value may get change outside this program, so declaring it as volatile to ensure that its changes are noticed by all threads volatile int flag = 0; int main() { cout << "Initial flag: " << flag << endl; flag = 1; cout << "Flag after change: " << flag << endl; return 0; }
Output
Initial flag: 0 Flag after change: 1
3. The mutable Qualifier
This qualifier gives access to change or modify data member values of a constant containing object. Therefore, when you declare the data members as mutable, you get access to change their values even if they belong to a const object.
Syntax
Here is the following syntax of declaring variable as mutable, which gives you access to change data members of a constant object.
mutable data_type variable_name;
Example code
Here is the following example in which we declared visits variable as mutable, so we are still able to increment its value even though it belongs to constant object d.
#include <iostream> using namespace std; struct Data { // this value can get change even if object is const mutable int visits = 0; void visit() const { // here we are still able to increment it's value because visits is mutable visits++; cout << "Visited " << visits << " times\n"; } }; int main() { const Data d; d.visit(); d.visit(); return 0; }
Output
Visited 1 times Visited 2 times