I love "const". I wish every variable and method that "ought to be const" IS "const". The problem is that whether a variable or method "ought to be const" depends on methods/variables further down in the call tree. Is there some tool, or some process, for statically examining a body of code and doing "bottom-up en-const-ification"?
A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type.
Yes. const is there in C, from C89.
In C or C++, we can use the constant variables. The constant variable values cannot be changed after its initialization.
In general, const is a better option if we have a choice and it can successfully apply to the code. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text.
I don't know the answer to your question, but I would like to argue against the claim that
whether a variable or method "ought to be const" depends on methods/variables further down in the call tree
Actually, const should be on a logical level. I.e. you should mark something const if it should not be changed logically. If it later is, then you will get a compiler error and will need to reconsider either the fact of changing or your initial assumption.
The rule is:
if something is const, it should not be changed
rather than
if something is not changed de facto, then let's make it const
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With