我是否应该声明任何可以为 const 的方法
简单的问题。 我应该声明任何可以为 const 方法的方法吗?
这包括不返回任何成员变量或返回对成员变量的 const 引用的方法。是否有任何理由不这样做(除了明显的原因,编译器无论如何都会指出)?
Simple question. Should I declare any method that can be const a const method?
This includes methods that don't return any member variables, or return const references to member variables. Is there any reason not to do so (apart from the obvious reasons, which the compiler will point out anyway)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不能通过指向常量对象的指针调用非 const 方法。因此,如果方法可以是
const
,那么不声明它为const就会对其使用施加人为限制。除此之外,使方法成为 const 是一个重要的语义细节,可以让用户感受到调用它所预期的效果。
A non-
const
method cannot be called through a pointer to a constant object. So if method can beconst
, not declaring it const would impose artificial limits on its use.Apart from that, making a method
const
is an important semantic detail to give the user a feeling of the effect that could be expected from calling it.看来我是这里的不和谐音符:
不,应该在设计过程中在不同的层面上做出决定。您应该将所有语义不修改对象的方法标记为
const
。这可能包括一些实际上修改一些不属于对象可感知状态的内部细节的方法(并且这些属性应该是可变的),并且可能不包括有些方法根本不会改变任何东西。在当前形式中,您无法更改实现,并且
change_impl
是非常量,即使它不修改任何成员属性,它也不会标记为const
,因为 < em>语义上它确实改变了。另一方面,expensen_calculation() 方法不会在语义上修改对象的状态,在调用该操作之前和之后可感知的状态将是相同的,但是它确实修改了
cache
属性以加快以后的调用速度(如果状态没有改变)。因此,该方法是const
,而缓存是mutable
。It seems that I am to be the discording note here:
No, the decision should be taken at a different level, during design. You should mark as
const
all methods that semantically don't modify the object. This might include some methods that do actually modify some internal detail that is not part of the perceivable state of the object (and those attributes should bemutable
) and it might not include some methods that do not change anything at all.In its current form, you cannot change the implementation, and
change_impl
is non-const, even if it does not modify any member attribute it is not marked asconst
, because semantically it does change.On the other hand, the
expensive_calculation()
method does not semantically modify the state of the object, the perceivable state will be the same before and after the operation is called, but it does modify acache
attribute to speed up later calls (if the state has not changed). As such, the method isconst
, and the cache ismutable
.是的。根据Effective C++,“尽可能使用const”。
Yes. According to Effective C++, "use const whenever possible".
如果方法不修改对象的逻辑状态,则应将方法标记为 const。这对于对象的客户端来说是一项很棒的服务,因为给定常量引用/指针,它们就可以得到最大程度的使用。
If a method does not modify the logical state of the object, then you should mark the methods as const. This is a great service to the clients of your objects, as they can then be used to the fullest extent, given const references/pointers.
在一种情况下,我会三思而后行地使用 const:基类虚函数。继承的类不能通过其重写的函数进行更改可能是一件好事,但一些开发人员可能不同意,并且因此必须克服困难。
There is 1 case where I would think twice about using const: a base class virtual function. It might be a good thing that inherited classes can't be changed by their overridden function, but some developers might disagree and really have to jump through hoops because of this.