这是真的,(常量牛逼V)将没有必要用C? [英] Is it true that (const T v) is never necessary in C?
问题描述
例如:
void func(const int i);
在这里,因为所有的参数通过值(包括指针)通过了常量
是不必要的。
是这样吗?
推荐答案
在C所有参数都确实是值传递的,这意味着实际的参数不会不管你是否包括常量更改
或没有。
All parameters in C are indeed passed by value, which means that the actual argument will not change regardless of whether you include that const
or not.
不过,这并不意味着常量
这里是从来没有必要的。是否有必要或不必要取决于你想要达到什么目的。
However, that does not mean that const
here is "never necessary". Whether it is necessary or unnecessary depends on what you want to achieve.
如果您想prevent任何试图修改函数内部的参数,那么常量
的是的必要,当然。
If you want to prevent any attempts to modify the parameter inside the function, then the const
is necessary, of course.
有一个相当受欢迎(和pretty合理)的编码指引,指出函数的参数不应该的功能,即,在函数的任何执行点的所有参数,必须保留其原始值内进行修改。根据这一方针,将实际上使完美意义的总是的包括常量
中的所有的参数声明。
There is a fairly popular (and pretty reasonable) coding guideline that says that function parameters should never be modified inside the function, i.e. that at any point of function's execution all parameters must retain their original values. Under this guideline it would actually make perfect sense to always include that const
in all parameter declarations.
这篇关于这是真的,(常量牛逼V)将没有必要用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!