Anand Hariharan said:
On Thu, 30 Jul 2009 02:59:41 +0000, Richard Heathfield
Unless one deals with pointer types and the const qualifies the
dereferenced value, I see little value in qualifying parameters with
const considering they are all pass-by-value anyway.
Taking Jacob's example, I will only be confused if I were to see the
prototype of fn as
int fn(const int a);
rather than what Jacob originally had viz.,
int fn(int a);
only because the implementor wanted to remind himself not to modify 'a'
within fn.
'const' should be used because it is part of the interface (and the
implementation is driven by the interface) rather than the implementation.
Well, you can always write:
int fn(int a); /* callers don't care whether it's modified */
/* ... */
int fn(const int a)
{
/* code that doesn't modify a */
}
My personal preference is to declare each object as "const" by
default, omitting the "const" only if I need to modify it after its
initialization. It makes it easier to reason about the behavior of
the code if I can be sure that the current value is the same as the
initial value without having to search for something that might have
modified it. But for parameters, this requires either making the
"const" uselessly visible in the prototype, or making the prototype
and the definition inconsistent.