I'm writing a class that wraps a legacy C API that controls a hardware device. 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. However, from a semantic point-of-view, would this be the correct approach, given that it affects the observable behaviour of the object? (i.e. the encapsulated hardware device very much does have state.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…