Can you guys please help me understand when to use const?
For example, if I have the following functions:

viod add (int* array)
{
    int total = array[1] + array[2];
}
viod subtract (int* array);
viod times (int* array);
viod divide (int* array);
viod printArray(int* array);

and I only intend to use the pointer to read from the dataset and not change any original values in the dataset. I understand that pointers can be used as pass by reference. So would it be a better programming practice to do the following?

viod add (const int* array);
viod subtract (const int* array);
viod times (const int* array);
viod divide (const int* array);
viod printArray(const int* array);

but I don't see people use const this way, adding const everywhere they can, let alone const pointers. Is there a reason for it?

Thanks for answering!

Edited 2 Years Ago by newbiewwcode: clarify

'const' ...
in your examples ...

tells the compiler that the values in the array are NOT to be changed ... and the compiler will warn you then, if you / your program ... later ... tries to change any values in the array ...

Try compiling some code, that -> inside a function that has a const array passed in ... then attempts to change the values in the array ... and then SEE what your compiler reports back.

Edited 2 Years Ago by David W: changed text

Thanks for the reply. I know what const does. My question is, a lot of people usually don't use const with pointers even when they don't intend to change what the pointer points to, and I just want to know why that is.

Maybe ... they just have not yet learned about C++ "const correctness" ?

The use of const seems to be left ... till later ... for many students :)

Edited 2 Years Ago by David W: changed comments

I don't see people use const this way, adding const everywhere they can, let alone const pointers. Is there a reason for it?

Probably best to ask the author of such code their reasoning. I can think of a few reasons ranging from not understanding best practice, to encountering errors when adding const and then removing it rather than fixing the errors, to outdated programming habits.

lot of people usually don't use const with pointers even when they don't intend to change what the pointer points to, and I just want to know why that is.

This is mainly a reflection of the ineptitude or laziness of many programmers in C++. First of all, const-correctness is something that is very unique to C++ (and D), and doesn't really exist in its complete form in any other language (it exists in C (since C90) but it's limited, and there are some minor features in Java and C# that attempt to mimic it but they are far too limited to be used in the same way). Even in C, using const is not too popular for historical reasons (lots of C programmers and code dates back to before 1990). In other words, anyone that is coming to C++ from almost any other language is probably not familiar with or used to writing const-correct code.

Another reason is that some people lack the technical know-how to write const-correct code. I mean that once in a while, you have to bend the rules a little bit, either because you are interfacing with an older library, or because you need to modify some data member within a const member function. C++ gives you some tools to bend the const-ness rules, in particular, the const_cast and the mutable keyword (to apply to data member that are "exempt" from the constness of their containing objects). If you don't know how to use those properly, you might be tempted to just remove the const keywords everywhere.

With that, hard habits are hard to break, and this is especially true with const-correctness because if you already have a large code-base which is not const-correct at all, then it is very hard to make it const-correct. This is because const-ness propagates (i.e., a const object cannot be de-const-ified, unless you use const_cast). This means that if you add a small part to that code that is const-correct, then it will clash very heavily with everything else in that library. The only choice is to do a major revision of the entire code-base and add const keywords in every place where it's appropriate (and potentially change some code as a result).

But writing good const-correct code is a fundamental part of good coding practices in C++. And it's really not that hard to live by, you just "make it const if you don't intend to modify it", and that rule alone does 99% of the job, and the other 1% are the few corner cases where its not obvious if it should be const or not, if it should be mutable or not, or if a const_cast should be applied, but these are very rare.

Thanks Mike! That's a really detailed reponse. Helped me understand const better!

This question has already been answered. Start a new discussion instead.