Const Correctness in C++ - Constant Variables
(Page 2 of 4 )
Constant, according to the Free Online Dictionary, means: Unchanging in nature, value, or extent; invariable. This pretty much tells you all you need to know about them in the beginning. Basically, C++ inherited this from the C language. There already, a constant variable was nothing more than one whose value remained constant throughout the execution of the program once a value had been assigned to it upon initialization.
Because writing a program can be time-consuming, and programmers are naturally lazy and prefer to accomplish much without writing much, in C/C++ the constant keyword has been reduced to const. As C/C++ includes more than just plain variables, the const keyword has been introduced for pointers and references as well. Below I will show you an example of each one and explain them afterward.
1. int I = 1;
2. const int I = 2;
3. const int* I = 3;
4. int const * I = 3;
5. int* const I = 2;
6. const int* const I = 1;
7. const int& I = 2;
8. int& const I = 2;
9. typedef int_co_ref int&;
10.int_co_ref const I = 2;
Now there we have all of the combinations possible, but what does each of them mean? Well, the first one should be obvious if you are still reading this text. As for the second one the situation should be same. All we did was declare a variable that is constant-unchangeable and initialize it with the value 1. From the point of declaring the variable through any point where the variable's declaration is still valid (aka in the interior of the scoop (parenthesis - {} you declared), we can use it instead of writing the number 1.
The third example represents a pointer pointing to a const variable. Once it is created, you can't modify the variable to which it points, but you are free to assign to the pointer a different variable. Here we can also include the fourth as it is the same; the compiler lets us write the const keyword where we wish.
Now the fifth example is more intriguing. We create a pointer that is a const. That means that it won't be able to change the variable through the pointer. However, through a different pointer or through the variable itself we are free to make any changes. Simply put, we have a pointer that, once assigned, cannot be changed, but that applies only to the pointer and not to the variables related to it.
The last one is the const from all points of view. Put the second and fifth examples together and you will get one that looks just like the sixth. A const (unchangeable) pointer points to a constant variable. This is an excellent method to use if you want to be sure that the input data won't change in a function.
The const reference is something that isn't. Okay now I know that sounds awkward, but the truth is that you can't create one like this. Simply put, it isn't correct grammatically. And to underline my statement, the eighth row in the upper code snippet will return a syntax error. Of course you can create one with rows 9 and 10, but the compiler would ignore it. The const here is redundant and will be ignored by the compiler.
The the upper statements are all true because a reference in its essence is a binder to a variable. Once you bind it to one, you cannot bind it to another; it can't refer to another variable. The reference is in its nature const.
Next: Const in Functions and in Classes >>
More C++ Articles
More By Gabor Bernat