gpt4 book ai didi

c++ - Google 关于输入/输出参数作为指针的风格指南

转载 作者:可可西里 更新时间:2023-11-01 15:32:33 26 4
gpt4 key购买 nike

Google C++ Style Guide在输入参数(→ const ref, value)和输入输出或输出参数(→ 非 const 指针)之间明确区分(严格后跟 cpplint.py):

Parameters to C/C++ functions are either input to the function, output from the function, or both. Input parameters are usually values or const references, while output and input/output parameters will be non-const pointers.

还有:

In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers.

但我不明白为什么输入/输出参数(我把输出参数放在一边)不应该通过引用传递。在 stackoverflow 上有很多与此问题相关的主题:例如here , 接受的答案清楚地说

it's mostly about style

但是如果

you want to be able to pass null, you must use a pointer

那么,如果我想避免指针为空,那么总是需要一个指针有什么意义呢?为什么只对输入参数使用引用?

最佳答案

要求输出参数作为指针传递的原因很简单:

在调用点清楚地表明参数可能会发生变化:

foo(x, y);     // x and y won't be mutated
bar(x, &y); // y may be mutated

当代码库不断发展并经历增量更改并由可能始终不了解整个上下文的人员进行审查时,能够尽快了解更改的上下文和影响非常重要。所以有了这个样式规则,一个变化是否引入了突变就很清楚了。

关于c++ - Google 关于输入/输出参数作为指针的风格指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26441220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com