Top-level const doesn't influence a function signature

后端 未结 7 2040
悲哀的现实
悲哀的现实 2020-11-27 08:32

From the C++ Primer 5th Edition, it says:

int f(int){ /* can write to parameter */}
int f(const int){ /* cannot write to parameter */}

The

7条回答
  •  忘掉有多难
    2020-11-27 08:56

    As the comments say, inside the first function the parameter could be changed, if it had been named. It is a copy of the callee's int. Inside the second function, any changes to the parameter, which is still a copy of the callee's int, will result in a compile error. Const is a promise you won't change the variable.

提交回复
热议问题