为什么创建 const 函数很有用?
如果只能读取变量而不能写入(类变量),为什么将函数设置为 const 如此有用?
why is it so useful to make a function const if you only can read variables but not write(class variable)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您将 const 指针或 const 引用传递给类的实例,那么它只能调用该类的 const 方法(如果有)。
显然,如果您从不关心类型的常量正确性,那么您可以忽略这一点。
我想它也可能帮助编译器在某些情况下优化一些东西,尽管我对此表示怀疑,而且即使它确实有帮助,允许小的改进(如果有的话)来决定你如何编写代码在大多数情况下都是过早优化的情况情况。
If you pass something else a const pointer or const reference to an instance of your class then it can only call the class's const methods (if any).
Obviously, if you never bother with const-correctness with your types then you can ignore this.
I suppose it may also help the compiler optimize things in certain situations, although I am doubtful and, even if it did help, allowing that small improvement (if any) to dictate how you wrote your code would be a case of premature optimization in most situations.
这样您就不会“意外”修改类变量之一。这只是一种安全措施。
(如果您在确实修改类的任何数据成员的函数之后使用 const 关键字 - 无论是直接修改还是通过另一个函数调用 - 您将收到编译错误。
So that you do not "accidentally" modify one of the class variables. It is just a safety measure.
(If you use the const keyword after a function that does modify any data member of the class - either directly or through another function call - you will get a compilation error).
原因之一是
const
是一种病毒。这意味着如果部分代码是常量正确的,那么其余代码将无法与该部分进行互操作。如果您忽略常量正确性,那么您的类与其他库(从标准库开始)携手合作的机会就很小。
例如:
使用 codepad.org
兼容 stdlib 的比较运算符必须承诺参数不会被修改。如果对象在比较时实际上发生了变化,那么尝试对它们进行排序将是徒劳的。
另一个例子:您将无法通过 const 引用传递参数,这是传递大对象的传统方式。相反,您必须通过可修改的引用来传递参数。现在您将无法将任何类型的临时变量传递给您的函数。
One reason is that
const
is a virus. That means that if part of the code is const-correct, then the rest of the code won't interoperate with that part.If you ignore const-correctness, chances of your classes working hand-in-hand with other libraries (beginning with the standard library) are slim.
For example:
With codepad.org
A stdlib compatible comparison operator must make a promise that arguments are not modified. If objects actually were to change while they are compared, an attempt to sort them would be rather futile.
Another example: you won't be able to pass arguments by const reference which is the conventional way of passing large objects. Instead you'd have to pass arguments by modifiable references. Now you won't be able to pass temporaries of any kind to your functions.
如果你有一个 const 对象,它只允许 const 成员函数对其进行操作。
If you have a const object, it only allows const member functions to operate on it.
您可以用代码而不是文档来表达调用者和被调用者之间的契约越多,编译器可以为您提供越多的帮助来遵守该契约(从两端)。隐式
this
指针的 const 性是其中的重要组成部分,就像所有其他参数所指对象的 const 性一样。 (当然,按值传递参数的顶级常量不是合约的一部分)The more of the contract between caller and callee you can express in code instead of documentation, the more help the compiler can give you with complying with that contract (from both ends). const-ness of the implicit
this
pointer is an important part of that as is const-ness of all other parameter referents. (of course top-level constness of pass-by-value parameters is not part of the contract)好处是您可以让编译器强制执行可以修改状态的位置。例如,如果您使用私有数据创建一个类,并且其所有方法(例如构造函数除外)都是 const,那么您就拥有了不可变的数据类型。这样做的好处不是性能方面的,而是语义方面的。
The benefit is that you can get the compiler to enforce where state can be modified. For example if you make a class with private data and all its methods, except, say, the constructor, are const, then you have an immutable data type. The benefit of this is not one of performance, but one of semantics.