C++:我应该在函数参数中尽可能多地使用常量吗?

发布于 2024-10-08 00:09:23 字数 171 浏览 6 评论 0原文

或者说这毫无意义?

谢谢。

编辑:我看到 char 数组在函数参数中经常保持不变......有什么原因吗?

编辑2:别介意我的编辑。请参阅“In silico”的答案。

编辑 3:澄清一下,这是一个礼仪问题,而不是“将会发生什么”问题。我明白 const 的作用。

Or is it pointless?

Thanks.

Edit: I have seen char arrays often constant in function arguments... is there a reason for that?

Edit 2: Nevermind my edit. See 'In silico's answer.

Edit 3: TO CLARIFY, this is an etiquette question, not a "what is going to happen" question. I understand what const does.

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

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

发布评论

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

评论(3

纵山崖 2024-10-15 00:09:24

考虑这个函数声明:

void Foo(const char* str);

它声明了一个名为 foo 的函数,它接受指向 const char 的指针。此上下文中的 const 意味着该函数承诺不会更改 str 指向的内容的内容。

它与 const- Correctness 有关,是该语言的基本部分。事实上,您必须要求相对简单的东西,这意味着您应该拿起一本很好的 C++ 书并通读它。您将更好地了解如何使用它以及我们为何要费心使用它。

另外,请阅读 此处此处。再次强调,它是该语言的基本部分,如果您想使用正确的 C++ 进行编码,正确使用它非常重要。

Consider this function declaration:

void Foo(const char* str);

This declares a function called foo that accepts a pointer to a const char. The const in this context means that the function promises to not change the contents of the stuff pointed by str.

It has to do with const-correctness and is a fundamental part of the language. The fact that you had to ask for something relatively simple means that you should go pick up a good C++ book and read through it. You'll gain a much better understanding of how to use it and why we even bother with it.

Also, read up on const-correctness here and here. Again, it's a fundamental part of the language and it's important that you get it right if you want to code in proper C++.

溇涏 2024-10-15 00:09:24

每当您处理引用 (&) 并且不打算更改参数时,请使用 const。这是为了清楚地将它们标识为输入参数。性能也应该更好。还有数以千计的其他原因。它对于按值传递的参数可能毫无用处,但也没有什么坏处。

Use const whenever you are dealing with references (&) and do not intend to change the argument. This is, to clearly identify them as input arguments. Performance should be better, too. And thousands of other reasons. It is probably useless on arguments handed over by value, but doesn't hurt either.

忘羡 2024-10-15 00:09:24

const 是您不会更改相关内容的承诺。您可以承诺不更改参数,不更改指针指向(或引用引用)的内容,不更改 this 对象(在成员函数中)等对于返回值,“承诺”对调用者具有约束力,因此实际上更像是不允许调用者更改返回的内容。

这对于指针和引用(尤其是引用,因为在 C++ 中,如果不需要的话,我们不喜欢通过指针传递)比对于值更有用,因为通过值传递无论如何都会产生一个副本;调用者并不关心被调用函数是否更改了它自己的值副本。但如果我们通过引用传递,const 让我们知道该函数不会通过引用更改我们的值。

类似地,当我们通过引用返回某些内容时,我们通常指的是预先存在的内容 - 也许最常见的是,我们通过成员函数的引用返回一些数据成员。在这种情况下,const 阻止调用者使用此引用来修改对象。这再次使得推理代码的作用变得更加容易,并防止错误。通常会看到成对的访问器函数:一个非常量版本,和一个返回值和 this-object 都是 const 的 const 版本。 (我们通过将 const 放在函数签名的末尾、参数列表的右括号之后之后,使 this-object 成为常量。)含义:“只要调用者不通过返回的引用改变对象,调用此函数就不会影响对象”。如果调用者有该类的 const 实例,则只能返回 const 引用。

有关更多信息,请阅读 http://www.parashift.com/c++- faq-lite/const- Correctness.html

const is a promise that you won't change the thing in question. You can make promises not to change a parameter, not to change the thing that a pointer points at (or a reference refers to), not to change the this -object (in a member function), etc. For a return value, the "promise" is binding upon the caller, so it's actually more like you're disallowing the caller to change the returned thing.

This is more useful with pointers and references (especially references, since in C++ we prefer not to pass by pointer if we don't have to) than with values, since passing by value makes a copy anyway; the caller doesn't care if the called function changes its own copy of a value. But if we pass by reference, const lets us know that the function won't change our value via the reference.

Similarly, when we return something by reference, we're generally referring to a pre-existing thing - perhaps most commonly, we return some data member by reference from a member function. In this case, const prevents the caller from using this reference to modify the object. That, again, makes it easier to reason about what the code does, and protects against mistakes. It's common to see accessor functions in pairs: one non-const version, and one const version where both the return value and the this-object are const. (We make the this-object const by putting const at the end of the function signature, after the closing parenthesis of the parameter list.) The meaning: "calling this function will not affect the object, as long as the caller doesn't mutate the object via the returned reference". If the caller has a const instance of the class, then only a const reference can be returned.

For more, please read http://www.parashift.com/c++-faq-lite/const-correctness.html .

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