according to some tutorials i read a while back, the "const" declaration makes a variable "constant" ie it cannot change later.
But i find this const declaration abit inconveniencing since the compiler sometimes gives errors like
"cannot convert const int to int"
or something like that.
and i find myself cheating by removing it anyway.
question: assuming that i am careful about not changing a variable in my source code, can i happily forget about this const stuff?
Thanks in advance
Its always optional. If its all your code sure you can forget it ( I wouldn't recommend it, because it protects you), but when you interact with others, you're essentially providing a contract for them that you won't change their object or calling a function does not change the state of your object. This can be invaluable when you are not familiar with other's code, or you don't have the source.
Changing things that shouldn't be changed is one of the most common sources of error. It is therefore worthwhile specifying const because it prevents you from doing something wrong. Why would you want to give that up?
There are some problems with the c++ notation, because a constant can only be initialized, not assigned the first time, and sometimes, you don't have the value at initialization time.
The way out of this one is to define a private function that computes the value of num but sometimes that means that instead of one clean block of code in the constructor, you are forced to split it into sections in awkward ways, just so you can initialize the variable.
Sometimes, you have a value that is generally supposed to be const, but you want to selectively override that when it semantically makes sense. For example, social security numbers don't change, unless your identity is stolen. So you have just one method, called createNewSSN() which changes the otherwise constant ssn
You can forget about it but isn't it nice if the compiler forces it upon you? That way, your "const" variables actually stay constant.
People usually face this problem when they start using the const keyword. Believe me, it really helps. Leave it to the compiler to take care of the cosntness of the variable, instead of you taking care to not alter its value anywhere.
The idea behind using 'const' is specifically that you ensure compiler errors when attempting to alter a variable's value when it has been predetermined (by using
const
) that you do NOT want to do so. It's essentially built in error-checking, and is useful for many reasons.This is especially valuable in cases such as an external interface and public methods as a way of guaranteeing the caller that a passed value will not be modified.
const
also locks in the intent to not modify, and prevents accidental assignment.While making
const
use mandatory is unnecessary, it is very useful and good practice.Here's a useful explanation you may want to check out: http://duramecho.com/ComputerInformation/WhyHowCppConst.html
Even if you and everyone you work with never makes mistakes, the use of const in your method declarations helps to document your interface.