This question is inspired by a question about final
in
java.
In C/C++, should I use const
whenever possible?
I know there is already a related question about using const
in parameters. Unfortunately that question and it's answers don't fully answer my question, because it's only about function parameters, but I would also like to know about other cases (eg: local variables).
Also, almost all answers to that question say we should use const
because it contains useful information about the accessibility of variables. But this seems to conflict with an answer about using final in Java which states final
may be superfluous if it doesn't contain extra information and so it should be omitted to keep the code short and clean.
So, should I use const
whenever possible? If so, why is the advice for const
in C++ different from the advice for final
in Java?