For example, imagine a simple mutator that takes a single boolean parameter:
void SetValue(const bool b) { my_val_ = b; }
Does that const
actually have any impact? Personally I opt to use it extensively, including parameters, but in this case I wonder if it makes any difference.
I was also surprised to learn that you can omit const
from parameters in a function declaration but can include it in the function definition, e.g.:
.h file
void func(int n, long l);
.cpp file
void func(const int n, const long l) { /* ... */ }
Is there a reason for this? It seems a little unusual to me.
Best Answer
Wrong.
It's about self-documenting your code and your assumptions.
If your code has many people working on it and your functions are non-trivial then you should mark
const
any and everything that you can. When writing industrial-strength code, you should always assume that your coworkers are psychopaths trying to get you any way they can (especially since it's often yourself in the future).Besides, as somebody mentioned earlier, it might help the compiler optimize things a bit (though it's a long shot).