Const Keyword

Constant is something that doesn't change. In C and C++ we use the keyword const to make program elements constant. const keyword can be used in many contexts in a C++ program. It can be used with:

  1. Variables
  2. Pointers
  3. Function arguments and return types
  4. Class Data members
  5. Class Member functions
  6. Objects

1) Constant Variables

If you make any variable as constant, using const keyword, you cannot change its value. Also, the constant variables must be initialized while they are declared.

int main
{
    const int i = 10;
    const int j = i + 10;     // works fine
    i++;    // this leads to Compile time error   
}

In the above code we have made i as constant, hence if we try to change its value, we will get compile time error. Though we can use it for substitution for other variables.


2) Pointers with const keyword

Pointers can be declared using const keyword too. When we use const with pointers, we can do it in two ways, either we can apply const to what the pointer is pointing to, or we can make the pointer itself a constant.


Pointer to a const variable

This means that the pointer is pointing to a const variable.

const int* u;

Here, u is a pointer that can point to a const int type variable. We can also write it like,

char const* v;

still it has the same meaning. In this case also, v is a pointer to an char which is of const type.

Pointers to a const variable is very useful, as this can be used to make any string or array immutable(i.e they cannot be changed).


const Pointer

To make a pointer constant, we have to put the const keyword to the right of the *.

int x = 1;
int* const w = &x;

Here, w is a pointer, which is const, that points to an int. Now we can't change the pointer, which means it will always point to the variable x but can change the value that it points to, by changing the value of x.

The constant pointer to a variable is useful where you want a storage that can be changed in value but not moved in memory. Because the pointer will always point to the same memory location, because it is defined with const keyword, but the value at that memory location can be changed.

NOTE: We can also have a const pointer pointing to a const variable.

const int* const x;

3) const Function Arguments and Return types

We can make the return type or arguments of a function as const. Then we cannot change any of them.

void f(const int i)
{
    i++;    // error
}

const int g()
{
    return 1;
}

Some Important points to Remember

  1. For built in datatypes, returning a const or non-const value, doesn't make any difference.
    const int h()
    {
        return 1;
    }
    
    int main()
    {
        const int j = h();
        int k = h();
    }
    Both j and k will be assigned the value 1. No error will occur.
  2. For user defined datatypes, returning const, will prevent its modification.
  3. Temporary objects created while program execution are always of const type.
  4. If a function has a non-const parameter, it cannot be passed a const argument while making a call.
    void t(int*) 
    { 
        // function logic
    }
    If we pass a const int* argument to the function t, it will give error.
  5. But, a function which has a const type parameter, can be passed a const type argument as well as a non-const argument.
    void g(const int*) 
    {
        // function logic
    }
    This function can have a int* as well as const int* type argument.

4) Defining Class Data members as const

These are data variables in class which are defined using const keyword. They are not initialized during declaration. Their initialization is done in the constructor.

class Test
{
    const int i;
    public:
    Test (int x)
    {
        i = x;
    }
};

int main()
{
    Test t(10);
    Test s(20);
}

In this program, i is a constant data member, in every object its independent copy will be present, hence it is initialized with each object using the constructor. And once initialized, its value cannot be changed.


5) Defining Class Object as const

When an object is declared or created using the const keyword, its data members can never be changed, during the object's lifetime.

Syntax:

const class_name object;

For example, if in the class Test defined above, we want to define a constant object, we can do it like:

const Test r(30);

6) Defining Class's Member function as const

A const member function never modifies data members in an object.

Syntax:

return_type function_name() const;

Example for const Object and const Member function

class StarWars
{
    public:
    int i;
    StarWars(int x)    // constructor
    { 
        i = x; 
    }

    int falcon() const  // constant function
    { 
        /* 
            can do anything but will not
            modify any data members
        */
        cout << "Falcon has left the Base";
    }

    int gamma()
    { 
        i++; 
    }
};

int main()
{
    StarWars objOne(10);        // non const object
    const StarWars objTwo(20);      // const object

    objOne.falcon();     // No error
    objTwo.falcon();     // No error

    cout << objOne.i << objTwo.i;

    objOne.gamma();     // No error
    objTwo.gamma();     // Compile time error
}

Falcon has left the Base Falcon has left the Base 10 20

Here, we can see, that const member function never changes data members of class, and it can be used with both const and non-const objecta. But a const object cannot be used with a member function which tries to change its data members.


mutable Keyword

mutable keyword is used with member variables of class, which we want to change even if the object is of const type. Hence, mutable data members of a const objects can be modified.

class Zee
{
    int i;
    mutable int j;
    public:
    Zee()
    {
        i = 0; 
        j = 0;
    }
    
    void fool() const
    { 
        i++;    // will give error
        j++;    // works, because j is mutable
    }
};

int main()
{
    const Zee obj;
    obj.fool();
}