I do understand the meaning of const
for pointers or structures that have to be passed by reference to a function. However in the example:
void foo(const int a);
the variable a
is passed on the stack. There is no harm to the caller to modify the content of the stack so const
looks pretty useless in this situation.
Furthermore, if I cannot modify a
, I can still make a copy of a and change this copy:
void foo(const int a)
{
int b = a;
b++;
}
In which situation does the const
keyword will be useful when applied to a scalar function argument (not a pointer)?