Understanding correctly that messy keyword ‘const’​ in C++ (I)

Pablo Correa
Jun 10 · 4 min read

Part one: constant types.

In C++, the qualifier ‘const’ specifies a compile-time constraint that an object or variable cannot be modified. The compiler verifies that the object or variable never changes and stops you when you try to modify it or re-assign it. However, writing ‘const’ correct code is way more challenging than just using the keyword in some lines, and then allowing the compiler to figure out what makes sense. In this three-part guide, you will get some hints on how to use it properly.

During my early days as a C++ programmer, I did not pay so much attention to the keyword ‘const’. I did not even pay so much attention to constants when I was immersed into PASCAL and Delphi programming, maybe because I was still attached to some of my habits from my BASIC days. So for me, C++ ‘const’ was some esoteric qualifier that did not represent so much interest or value. If I defined a variable that I was only going to use inside the body of a function (and I was sure that nothing was not going to change it), why bother myself with making it a constant? Besides, C++ has very awkward and seemingly difficult usages of ‘const’ not worth discovering their meaning in my initial learning curve. Here is a funky example:

If you are confused, do not worry! I promise that we will solve it step-by-step. Even that it is a bit difficult, it is not like that famous quote that makes most C/C++ programmers brains’ melt.

So…are you ready? Let’s begin with the basics. The first thing to know is that when you declare any variable or object, the qualifier ‘const’ can be written before or after the type.

So the line of code above means for the compiler exactly the same as the following line:

Ok. That was not so hard, right? The rule above applies to anything you want to be constant, including arrays. So, the following lines mean for the compiler exactly the same:

However, for the sake of clarity, I always put the keyword ‘const’ first. That is the code style I am going to keep from now on.

Let’s move to the next level. You can declare variable pointers to constant types:

And of course, it does not matter if the reference value is a constant:

You can even have a constant array that points to constant types and increment the pointer:

However, if you point to a constant type, even the pointer may be changeable, it is not possible to use the dereference operator to change the value it points to. You will get a compiler error because you tried to make an assignment in a read-only location:

You can also declare a constant pointer to a variable type. The address stored in the pointer itself is constant, so the pointer has to be initialized when it is declared. Even that is possible to change later the value it points to, it is not possible to change the address itself:

Lastly, you can declare a constant pointer to a constant type. Since the stored address is constant, the pointer needs to be initialized in the declaration. Later, it is not possible to change the value it points to, nor to change the address:

Ok, so far we solved the first half of our funky example at the beginning of the article. Before we finish, there is the last thing to consider. It is possible to cast away constness, using const casts. They work very similarly to regular typecasts in C++, except that they can not make conversion between types or casting down a class hierarchy.

However, there is a better option for changing a constant variable that I will show you in the second part, where we are going to explore how to use constants inside the methods of a class. That’s it for now! Until the next one, happy coding!

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store