Also, it is pointless to pass val by const value. Either pass by value or by const reference.
While it may be the case for this example, I wouldn't agree that it is always pointless. Consider the following quick example.
1 2 3 4 5 6 7 8 9 10 11 12
void func(constint value)
{
// the compiler wouldn't catch this error if value wasn't a const int
if(value = 5) // should be value == 5
{
// do some things
}
else
{
// do other things
}
}
The point is that even when passing by value the const keyword is useful for situations where the parameter isn't going to be modified. There are plenty of situations where we create constants on the stack within the function so it is equally useful to simply declare the function parameter to be a const isn't it? I know that this is a rather old thread but I actually ran across a problem like this not long ago where the compile caught a silly mistake for me but only because I specified the input params to be const. I never used to specify const when passing by value but recently changed my mind about that. I figure that if a parameter is truly a read only parameter why not specify it to be a const? What's wrong with it?
No, my compiler never catches those types of things. If the extra typing bothers you, then you don't have to do it. I'm just saying that it isn't necessarily pointless to do it. I never used to do it until I saw it done by others and then realized that there is some value in doing it sometimes.