Type Qualifiers

Type Qualifiers

Linux Distribution
any text editor

My Setup:
Debian 10
g++ version 6.3.0

In my classes tutorial , I had talked about const briefly , but in this
tutorial, i will be going over all of whats known as “Type Qualifiers”. What is
a type qualifier? A type qualifier is a way of expressing additional information
about the value through the type system. In C++, there are currently 3 type

1. Const
Lets start off with the const qualifier. Objects declared as const may be
placed in read only memory by the compiler, and if it is never used within a
program it may be taken out completely. This means that the data can’t be
manipulated .

const char c = 'B'; //const char type , read only
c = 'C'; // error: type of c is const-qualified

float f = 9.37f;
const float *cf = &x; //assigning a non-const to const pointer
*cf = 5.39f; // error: the type of the lvalue *cf is const-qualified

Now there are ways to modify variables that are currently const, you can use
a const_cast as such:

const char c = 'B';
char *foo = const_cast<char*>(&c);

2. Volatile
The volatile type qualifier is almost on the completely opposite spectrum.
Declaring a variable volatile tells the compiler that the variable can be
changed externally through a signal or other processes. This in turn stops any
optimizations from happening to a variable. This is usually done when you need
to read from a port , since the data can constantly be changing, the variable
instead of reading once, and using the temporary variable , like optimizations
may do, it will constantly read the port . This is also suitable for
communicating with a signal handler, but not with another thread of execution.

3. Const Volatile
A const volatile variable acts as both a const and volatile qualified

Ok so this isnt getting a number because I am not sure if this is even
considered a type qualifier. However, if you need to modify a member of a const
object , you may use the mutable keyword.
The cppreference site has a really good example of this:

class X {
  mutable const int* p;         // OK
  mutable int* const q;         // ill-formed

class ThreadsafeCounter {
  mutable std::mutex m; // The "M&M rule": mutable and mutex go together
  int data = 0;
  int get() const {
    std::lock_guard<std::mutex> lk(m);
    return data;
  void inc() {
    std::lock_guard<std::mutex> lk(m);

For more information on Type Qualifiers you can check out

6 thoughts on “Type Qualifiers

  1. /*prg to change the value of a const variable */
    int main()
    const int a = 10;
    int *ptr = &a;
    *ptr = 77;
    printf(“a = %d\n”,a);
    return 0;

    1. that isnt const :
      prog.c:6:12: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
      int *ptr = &a;

      you essentially take out const when you do that. That is also C, in c++ you would get:
      prog.cpp:6:12: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
      int *ptr = &a;

      1. Yes we will get warning in “C” but not error.
        Please observe the value of “a” variable.It is modified.

      2. The variable a Is not const, the compiler took that away from the variable prior to the compilation. The compiler tells you that when it says: initialization discards ‘const’ qualifier.

Leave a Reply