Con.3:默认情况下,传递参照常量的指针或引用
To avoid a called function unexpectedly changing the value. It's far easier to reason about programs when called functions don't modify state.
为了避免被调用的函数意外修改变量的值。当被调用的函数不会修改状态时这么做会使程序的理解更加容易。
Example(示例)
void f(char* p); // does f modify *p? (assume it does)
void g(const char* p); // g does not modify *p
It is not inherently bad to pass a pointer or reference to non-const, but that should be done only when the called function is supposed to modify the object.
将指针或者参照传递给非常量也不是就一定不好,但是最好只有在被调用的函数会修改对象时这么做。
Note(注意)
Do not cast away const
Enforcement(注意)
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#con3-by-default-pass-pointers-and-references-to-consts