我应该声明这些方法const? [英] Should I declare these methods const?

查看:152
本文介绍了我应该声明这些方法const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些C ++代码,其中有几个经理对象与私人方法如

  void NotifyFooUpdated ; $     

这个对象的监听器。



请注意,它们不修改此对象的状态,因此他们可以在 const 方法,即使它们通常修改系统的整体状态。特别是,监听器对象可能会回调到这个对象并修改它。



我个人希望保留它们,而不是声明他们<$ c $然而,我们的静态代码检查器QAC标记这是一个偏差,所以我要么声明他们<$ c $

c> const ,或者我不得不争论为什么他们应该保持非const并获得一个偏差的补助。



没有声明这些方法 const

或者我应该跟随QAC并声明他们 const ?< br>
我应该采用严格局限的观点,限于这个对象,还是把系统作为一个整体?

解决方案

宽松地说,你有一个容器类:一个充满了观察者的经理。在C和C ++中,可以使用具有非常量值的const容器。考虑是否删除了一层包装:

  list< Observer> someManager; 

void NotifyFooUpdated(const list< Observer>& manager){...}


$ b b

你不会看到一个全局NotifyFooUpdated采用一个const列表没什么奇怪,因为它不修改列表。该const参数实际上使参数解析更容许:该函数接受const和非const列表。类方法版本上的所有const注释都是 const * this



p>


如果你不能保证调用函数的对象在函数调用之前和之后保持不变,你应该非const。


这只有在调用者只有对象的引用时才是合理的。如果对象是全局的(如在原始问题中)或在线程环境中,任何给定调用的常量不保证对象的状态在整个调用中不变。没有副作用并且对于相同输入总是返回相同值的函数是 pure 。 NotifyFooUpdate()显然不是纯粹的。


I'm working on some C++ code where I have several manager objects with private methods such as

void NotifyFooUpdated();

which call the OnFooUpdated() method on the listeners of this object.

Note that they don't modify the state of this object, so they could technically be made const methods, even though they typically modify the state of the system as a whole. In particular, the listener objects might call back into this object and modify it.

Personally I'd like to leave them as they are and not declare them const.

However, our static code checker QAC flags this as a deviation, so I either have to declare them const, or I have to argue why they should stay non-const and get a grant for the deviation.

What are arguments for not declaring these methods const?
Or should I follow QAC and declare them const?
Should I adopt a strictly local viewpoint restricted to this object, or consider the system as a whole?

解决方案

Loosely speaking you have a container class: A manager full of observers. In C and C++ you can have const containers with non-const values. Consider if you removed one layer of wrapping:

list<Observer> someManager;

void NotifyFooUpdated(const list<Observer>& manager) { ... }

You would see nothing strange about a global NotifyFooUpdated taking a const list, since it does not modify the list. That const argument actually makes the argument parsing more permissive: The function accepts both const and non-const lists. All the const annotation on the class method version means is const *this.

To address another perspective:

If you can't guarantee that the object you invoked the function on remains the same before and after the function call, you should generally leave that as non-const.

That's only reasonable if the caller has the only reference to the object. If the object is global (as it is in the original question) or in a threaded environment, the constness of any given call does not guarantee the state of the object is unchanged across the call. A function with no side-effects and which always returns the same value for the same inputs is pure. NotifyFooUpdate() is clearly not pure.

这篇关于我应该声明这些方法const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆