通过引用进行 const 传递是否比通过引用进行非 const 传递更好?

发布于 2024-08-12 06:37:34 字数 364 浏览 6 评论 0原文

我遇到过这个。

http://google-styleguide.googlecode.com/ svn/trunk/cppguide.xml?showone=Reference_Arguments#Reference_Arguments

根据样式指南,仅允许 const 引用作为参数。 (这就是我的理解)

虽然,我似乎不喜欢这种做法。

评论?

I came across this.

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Reference_Arguments#Reference_Arguments

According to the style guide, only const references are allowed as parameters. (That's what I understood)

Although, I don't seem to like that approach.

Comments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请爱~陌生人 2024-08-19 06:37:34

以下是在选择选项时有用的一些经验法则:

如果您不需要需要修改传递到函数中的对象,请考虑该对象的大小是否更小小于或等于指针的大小。如果是,则按值传递;否则,通过 const 引用传递。

如果您确实需要修改传递给函数的对象,请考虑该参数是否是可选的(即 null 是有效参数)。如果是,则通过指针传递;否则,传递非常量引用。

Here are a couple of rules of thumb that can be useful when deciding between your options:

If you don't need to modify the object being passed into the function, consider whether the size of the object is less than or equal to the size of a pointer. If so, pass by value; otherwise, pass by const reference.

If you do need to modify the object being passed into the function, consider whether the parameter is optional (that is, null is a valid argument). If so, pass by pointer; otherwise, pass by non-const reference.

谁与争疯 2024-08-19 06:37:34

Google 建议仅使用 const 引用,因为他们认为当函数可能修改对象时传递指针会更清晰。这可能是正确的,但我更喜欢只在 null 是可接受的值时才使用指针。

为了澄清这一点,这里有一个例子来解释他们的论点的根源。

Car c;
foo(c);

c 是否被函数修改完全取决于它的声明方式,仅查看调用本身并不能说明可能发生的情况。

void foo(Car c);  
void foo(const Car &c);  OR 
void foo(Car &c); 

现在考虑

Car c;
foo(&c);

传递对象的地址后,可以更轻松地(从调用者的角度)查看该函数是否可能正在更改您的对象。当然,这并不能保证它是指向 const 对象的指针,但从代码审查者的角度来看,当函数传递指针时,更容易检测到该对象可能会发生更改。当 Google 的建议得到严格执行时,通过指针传递的对象将始终是可变的,而任何不通过指针传递的对象都将是 const。 (通过 const & 或 b/c 传递值)

这是否更好是有争议的。我会决定你的感受并在你的团队中保持一致。

Google suggests only const references because they feel it is clearer to pass pointers when a function may modify the object. This is probably true, yet I prefer to only use pointers when null is a value that is acceptable.

To clarify, here is an example that explains the root of their argument.

Car c;
foo(c);

Whether c is modified by the function all depends on how it is declared and just looking at the call itself doesn't give much of an indication as to what could possibly happen.

void foo(Car c);  
void foo(const Car &c);  OR 
void foo(Car &c); 

Now consider

Car c;
foo(&c);

Having passed the address of your object, it is easier to see (from the callers perspective) whether or not the function may be changing your object. Granted, it is not a guarantee that it is as it could be a pointer to a const object, but from a code reviewers standpoint, it is easier to detect that this object may be changed as the function is passed a pointer. When Google's suggestion is strictly enforced, an object passed via a pointer would always be mutable, while anything not passed via a pointer would be const. (either by const & or b/c of pass by value)

Whether this is better or not is debatable. I would decide how you feel and be consistent across your team.

ゝ杯具 2024-08-19 06:37:34

它还使您可以执行

void x(const std::string& x)

x("hello");

没有 const 就行不通。

it also enables that you can do

void x(const std::string& x)

x("hello");

without the const it wouldn't work.

相守太难 2024-08-19 06:37:34

这是一种偏好,正如我可以说这是一种偏好:

void align_left();
// vs.
void alignLeft();

既然您想通过非常量引用传递参数,那么就这样做。我不喜欢传递指针。该约定通常还有其他含义(即许多程序员看到指针并认为可能不需要该参数,然后必须去检查文档)。因此,按照惯例,您通常会让参数名称表明它将改变参数。

t_result getDate(t_string& outString) const;

这个名字就表明了突变。如果您在代码库中很少通过 ref ,那么它可能有点欺骗性,但它最终是“好的”并且具有足够的积极影响。关于风格,很多都回到了一致性。随着程序的真正扩展,(IMO)这真的很有帮助 - 只需确保程序的意图清晰且风格一致(至少在编写庞大的代码库之前您愿意阅读样式文档)。

It's a preference, as I can say this is a preference:

void align_left();
// vs.
void alignLeft();

Since you want to pass arguments by non const references, then do so. I don't prefer passing pointers. The convention typically has other implications (i.e. many programmers see a pointer and think the parameter may not be required, and then have to go check the docs). So you, in convention, would typically make the parameter name indicate that it would mutate the argument.

t_result getDate(t_string& outString) const;

The name then indicates the mutation. It can be a little deceptive, if you pass by ref very rarely in your codebase, but it is ultimately 'fine' and has enough positive implications. Regarding style, a lot of it comes back to consistency. As programs really expand, (IMO) this can really help - just make sure the intent of your program is clear and the style is consistent (at least you care to read style docs before you write a huge codebase).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文