C++ const keyword


Welcome to the fascinating world of C++ programming, where even the smallest things matter a lot. Today, we’ll focus on the “const” keyword, which might seem simple, but it plays a crucial role in making C++ code reliable and easy to understand. In this article, we’ll explore the basic and practical uses of the “const” keyword, helping you see how valuable it is for your C++ learning journey. So, let’s dive in and discover the wonders of the “const” keyword together in a straightforward and approachable way!

In this article, we’ll explore the different scenarios where “const” can be used, accompanied by simple examples. From creating constants to working with pointers and member functions, we’ll take it one step at a time.

Constant variable.

This is simplest schenario you can encounter of usage of const keyword on C++. Basically it allows us to define a variable which is not variable 🙂 . By declaring variables as “const,” we ensure that their values remain fixed throughout the program’s execution.

const int MAX_VALUE = 100;
const double PI = 3.14159;

After you define a variable as a const, compiler will not allow you to change its value in anywhere else. Also, compiler/linker put this variable into rom rather than ram cause it is obvious for them that this variable wont be changed.

Constant variable with a pointer

Conversely, we’ll explore pointers to constants,which allow us to create pointers that point to unchangeable values via ptr pointer. But address of variable which means pointer can be changed.

int value = 42;
const int* ptr = &value;   // int const* ptr is also accepted.

Constant pointer for a variable.

Next, we delve into the world of constant pointers, where the pointer itself can not be modified, but the value it points is not constant.

int value = 42;
int* const ptr = &value;

be carefull about place we used const keyword. It is located just before pointer name after * sign.

Constant pointer for constant variable.

Lets make everything constant :). pointer and also value it points. By this way we can change neither pointer value nor value it points.

int value = 42;
const int* const ptr = &value;

Constant member function.

In the realm of object-oriented programming, we encounter the need for member functions that don’t modify the object’s state. Enter constant member functions, which offer a promise not to alter the object, allowing them to be called on constant objects.

class Circle {
public:
    double getArea() const;
    // other member functions...
};
Basically, getArea method can not make any modification on any Circle variable unless they defined as mutable.

Constant function parameters.

When you use “const” with function parameters, you establish a contract between the function and the caller, assuring that the data passed as arguments remains unchanged throughout the function’s execution. This assurance safeguards against inadvertent modifications and unexpected side effects, contributing to code safety and predictability. This makes more sense when you pass a variable by reference rather than by value.

void printArea(const Circle c);

If you try to call getArea method of Circle object inside printArea function. You will understand why do you need constant member function for getArea. Basically, if you dont define getArea as constant member function, you will have compilation error while defining printArea with a const parameter. By this way, We can understand why do we need them.

Leave a comment