[英]Should member functions be “const” if they affect logical state, but not bitwise state?
I'm writing a class that wraps a legacy C API that controls a hardware device. 我正在编写一个包含控制硬件设备的传统C API的类。 In a simplified example, I might have something like:
在一个简化的例子中,我可能有类似的东西:
class device
{
public:
void set_request(int data) { legacy_set_req(p_device, data); }
int get_response() const { return legacy_get_rsp(p_device); }
private:
device_handle_t *const p_device;
};
The class itself has no bitwise state; 这个类本身没有按位状态; therefore, I could choose to declare
set_request()
as const
, and the compiler would be happy with that. 因此,我可以选择将
set_request()
声明为const
,编译器会对此感到满意。 However, from a semantic point-of-view, would this be the correct approach, given that it affects the observable behaviour of the object? 但是,从语义的角度来看,这是否是正确的方法,因为它会影响对象的可观察行为? (ie the encapsulated hardware device very much does have state.)
(即,包封的硬件设备非常确实有状态)。
I believe that const
should reflect logical const-ness, regardless of the internal representation. 我相信
const
应该反映逻辑常数,而不管内部表示。 Just because your object contains only a pointer to something that changes, doesn't mean all your member functions should be const
. 仅仅因为你的对象只包含指向变化的东西的指针,并不意味着所有的成员函数都应该是
const
。
C++ even has the mutable
concept for internal representation that needs to change even if conceptually the object does not. C ++甚至具有内部表示的
mutable
概念,即使概念上对象没有,也需要改变。 The const
keyword is clearly not intended to represent "bitwise" const-ness. const
关键字显然不是为了表示“按位”常量。
If it changes the state, it generally should not be const
. 如果它改变了状态,一般不应该是
const
。 The fact that the state in question is owned remotely (ie, in a controlled device) doesn't change that. 有问题的状态是远程拥有的(即,在受控设备中)并不会改变这一点。
A useful test is: 一个有用的测试是:
could code with only const
access to the device instance interfere with the operation of code with non- const
access 只能对设备实例的
const
访问进行编码会干扰非const
访问的代码操作
Basically, const
access is meant to mean you're effectively an observer. 基本上,
const
访问意味着你实际上是一个观察者。 Here, it looks as though code calling set_request(...)
at time T1 then get_response()
at time T3 would be seriously screwed if some supposed observer called set_request()
with different parameters at time T2. 在这里,如果一些假设的观察者在时间T2用不同的参数调用
set_request()
,那么看起来好像在时间T1处调用set_request(...)
然后在时间T3处调用get_response()
代码将被严重搞砸。 For that reason, set_request(...)
shouldn't be accessible to observers - it should be non- const
. 出于这个原因,
set_request(...)
不应该是观察员访问-它应该是不const
。
(There are caveats to the test - eg if a const
"observer" needs a lock it can obviously affect non- const
usage from another thread from a timing perspective but shouldn't do so from a functional one - but it's pretty obvious how to factor that into your analysis). (有警告的测试-例如,如果一个
const
“观察者”需要锁可明显影响非const
从时序的角度另一个线程的使用,但不应该从功能一个这样做-但它是很明显如何进入你的分析的因素)。
Like other type and member qualifications (eg, public, private, virtual), const expresses both intention and the language semantics (ie, safety features) that support that intention. 与其他类型和成员资格(例如,公共,私有,虚拟)一样,const表达支持该意图的意图和语言语义(即安全特征)。 In this case, the intention would appear counter-intuitive, even if the underlying semantics would be safe.
在这种情况下,即使潜在的语义是安全的,意图也会显得违反直觉。 I wouldn't do it.
我不会这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.