Difference between const reference and normal parameter

前端 未结 7 1997
我寻月下人不归
我寻月下人不归 2020-11-28 01:09
void DoWork(int n);
void DoWork(const int &n);

What\'s the difference?

7条回答
  •  不知归路
    2020-11-28 01:48

    Since none of you mentioned nothing about the const keyword...

    The const keyword modifies the type of a type declaration or the type of a function parameter, preventing the value from varying. (Source: MS)

    In other words: passing a parameter by reference exposes it to modification by the callee. Using the const keyword prevents the modification.

提交回复
热议问题