Home arrow C++ arrow Page 2 - Const Correctness in C++

Const Correctness in C++

One of the biggest advantages of programming in C++ is that you can do the same thing in a variety of ways. At least in theory the compiler lets you do this. Under these circumstances, the difference between a good programmer and a very good programmer lies in the methods they use to accomplish specific tasks. Coding with const correctness in mind is one of the recommended techniques that raises you to the level of a very good programmer. Itís that important to grasp this concept.

Author Info:
By: Gabor Bernat
Rating: 3 stars3 stars3 stars3 stars3 stars / 6
October 28, 2008
  1. · Const Correctness in C++
  2. · Constant Variables
  3. · Const in Functions and in Classes
  4. · Conclusion

print this article

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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials