What Every C++ Developer Should Know to (Correctly) Define Global Constants--Jonathan Boccara

C++17 to the rescue.

What Every C++ Developer Should Know to (Correctly) Define Global Constants

by Jonathan Boccara

From the article:

Constant values are an everyday tool to make code more expressive, by putting names over values.

For example, instead of writing 10 you can write MaxNbDisplayedLines to clarify your intentions in code, with MaxNbDisplayedLines being a constant defined as being equal to 10.

Even though defining constants is such a basic tool to write clear code, their definition in C++ can be tricky and lead to surprising (and even, undefined) behaviour, in particular when making a constant accessible to several files.

Everything in this article also applies to global variables as well as global constants, but global variables are a bad practice contrary to global constants, and we should avoid using them in the first place...

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.