Isn't “const” redundant when passing by value? [duplicate]

二次信任 提交于 2019-11-29 21:58:47
Ernest Friedman-Hill

The const qualifier prevents code inside the function from modifying the parameter itself. When a function is larger than trivial size, such an assurance helps you to quickly read and understand a function. If you know that the value of side won't change, then you don't have to worry about keeping track of its value over time as you read. Under some circumstances, this might even help the compiler generate better code.

A non-trivial number of people do this as a matter of course, considering it generally good style.

psur

You can do something like this:

int f(int x)
{
   x = 3; //with "const int x" it would be forbidden

   // now x doesn't have initial value
   // which can be misleading in big functions

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!