常量成员函数

发布于 2024-10-12 04:44:18 字数 241 浏览 4 评论 0原文

阅读 this 后,我的理解是将方法声明为 const防止它意外修改类的成员变量。

  • const 方法常用吗?
  • 它们是否应该用于不应该修改成员变量的一切

After reading this, it is my understanding that declaring a method as const prevents it from accidentally modifying the class's member variables.

  • Are const methods commonly used?
  • Should they be used for everything that shouldn't modify member variables?

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

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

发布评论

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

评论(8

蹲墙角沉默 2024-10-19 04:44:18

是的,在适当的时候应该始终使用const

它可以让您的编译器检查您的应用程序逻辑,静态断言 const- Correctness 免费!

有些人甚至说 const 应该是默认值,你应该被迫使用 mutable 来处理非常量。

Yes, const should always be used, when appropriate.

It lets your compiler check your application logic, statically asserting const-correctness for free!

Some people even say that const should be the default, and you should be forced to use mutable for what is non-constant.

和影子一齐双人舞 2024-10-19 04:44:18

我广泛使用 const 来传达设计意图。如果我想让一个方法是一个纯粹的查询,而不是一个修改函数,我会强制执行它并在签名中使用“const”进行通信。

我鼓励您了解 Meyer 关于无副作用函数的想法,以及它对启用测试的影响。

I use const extensively to communicate design intent. If I intended that a method was a pure query, and not a modification function, I would both enforce that and communicate it with a 'const' in the signature.

I encourage you to look at Meyer's thoughts on the matter of side-effect-free functions, and the implications that has on enabling testing.

冷清清 2024-10-19 04:44:18

只是我的见证。

几年前,我仍然反对使用 const,只是因为设计和编写更长的函数签名方面的限制......等等......

但我的一位项目负责人始终坚持,一直提醒我: “你应该使用 const 函数,它可以避免意外和无意义的事情”。

有一天,我遇到了一个不可能发现的错误。日复一日,日复一日……一场噩梦。这个设计对我来说太大了,所以我无法从整体上掌握它。我徒劳地寻找,直到我确定我迷路了。

然后我花了两天时间重新定义所有应该是const的函数。我是说,两天。 (重新编译的时间很长,因为这是一个 500 万行代码的项目)。

然后:只是我发现了这个错误......而不是编译器为我找到了这个错误:在一个类似 getter 的方法中,它应该给我一个 gui 控件的首选大小,代码实际上是在计算大小,但它还缓存其大小并更新其大小...从而修改对象。

现在,有时我忘记添加 const。但如果我注意到它,我就会纠正它。

Just my testimony.

Some years ago, I was still against the use of const, just because of the constraints in design and writing longer function signatures... and so on...

But one of my project leaders always insisted, all the time, reminding me: "You should use const function, it avoids accidents and doing non-sense".

And one day I faced an impossible bug to find. Days after days after days... A nightmare. The design was too big for me too see so as I could grasp it in its whole. I searched in vain until I decided I was lost.

Then I spent two days redefining ALL the functions that should be const. I mean it, two days. (Recompilations were long as it was a 5 millions lines of code project).

And then: simply I found the bug... rather the Compiler found the bug for me: In a getter-like method, which should have given me the prefered size of a gui control, the code was actually computing the size, but it was also caching its size and updating its size... Thus modifying the object.

Now, sometimes I forget to put const. But if I notice it, I correct it.

℡Ms空城旧梦 2024-10-19 04:44:18

将 const 添加到成员函数允许在对象的 const 引用上调用它,因为它保证实例变量不会被更改。 Const 引用出现在整个 STL 的各个位置,因此在函数不打算修改对象状态的情况下将成员函数标记为 const 是有意义的。

注意:可以将某些实例变量标记为可变的,这样它们甚至可以通过 const 函数进行更改。例如,这对于实现查找缓存很有用。

Adding const to a member function allows it to be called on const references to an object, because it guarantees that instance variables won't be changed. Const references occur in various places throughout the STL, so it makes sense to mark member functions as const where the function doesn't intend to modify the state of an object.

Note: it is possible to mark certain instance variables as mutable so they can be changed even by const functions. This is useful to implement look-up caches, for example.

泅人 2024-10-19 04:44:18

声明一个不应修改成员变量的方法:

  1. 确保您所认为的就是正在发生的事情,即您不会意外地在某处修改变量。
  2. 向函数的调用者声明此方法不会修改成员变量,从而无需阅读代码或依赖这样说明的文档。

所以,是的,只要有意义就使用 const。但它们并没有像我希望看到的那样广泛使用,很可能是因为大多数开发人员没有看到巨大的好处。

Declaring a method that shouldn't modify member variables:

  1. Assures that what you think is what is happening, i.e. that you're not accidentally modifying a variable somewhere.
  2. Declares to callers of the function that this method doesn't modify member variables, removing the need to read over the code or rely on documentation that says so.

So yes, use const wherever it makes sense. They're not as widely used as I'd like to see though, most likely because the majority of developers don't see the huge benefit.

心欲静而疯不止 2024-10-19 04:44:18

如果您忘记将访问器标记为 const,编译器将不允许在 const 对象或对 const 对象的引用上调用该方法。所以,是的,将访问器标记为 const 很重要。

If you forget to mark an accessor as const, the compiler will not allow the method to be called on const objects or references to const objects. So yes, marking accessors as const is important.

趴在窗边数星星i 2024-10-19 04:44:18

如果你有一个类对象的 const 引用或指针(即指向 const 的指针),那么你只能调用该类的 const 成员方法。

因此,如果有人“忘记”将“get”方法设置为 const,您将无法使用 const 引用来调用它(有 const_cast 的解决方法,但我们不想使用它!)。

所以是的,如果该类不会被该方法修改,那么它应该是 const。

注意:在某些情况下,您确实希望将变量修改为“实现细节”,例如延迟加载或锁定互斥锁。在这种情况下,您仍然可以将该方法设置为 const,但将该成员变量设置为“可变”。

如果您正在编写虚拟方法,并且没有派生类需要它是可变的,则它应该是 const。

If you have a const reference or pointer (i.e. pointer to const) of a class object then you can ONLY call const member methods of the class.

So if someone "forgot" to make a "get" method const, you would not be able to call it with a const reference (There is a workaround with const_cast but we don't want to use that!).

So yes, if the class is not going to be modified by the method then it should be const.

Note: there are some occasions that you do want to modify a variable as an "implementation detail", eg lazy-loading or to lock a mutex. In such a case you can still make the method const but make that member variable "mutable".

If you are writing a virtual method, it should be const if no derived class will need it to be mutable.

走野 2024-10-19 04:44:18

您应该尽可能使用 const 关键字。

它可以防止代码中出现错误。

它大大提高了代码的可读性。
每个阅读标题并看到 const 关键字的人都可以立即理解 const 方法不会更改对象的状态,并且可以使用而不用担心他会更改对象,例如

You should use the const keyword whenever possible.

It prevent you from the mistakes in the code.

It increase the readability of the code very much.
Everyone who is reading the header and seeing const keywords can immediately understand that a const method does not change the state of an object and can be used with no being afraid he will change the object for example

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