tags:

views:

72

answers:

3
+2  Q: 

Being observable

What does it mean: Object's observable state?
I was reading yesterday in "Exceptional C++" Solution to item 43 and there is a fragment:

private: void InvalidateArea() { area = -1; }

Even though this function modifies the object's internal state, it should be const. Why? Because this function does not modify the object's observable state. We are doing some caching here, but that's an internal implementation detail and the object is logically const even if it isn't physically const.

Corollary: The member variable area should be declared mutable. If your compiler doesn't support mutable yet, kludge this with a const_cast of area_ and write a comment telling the next person to remove the const_cast once mutable is available—but do make the function const._

As usual thanks for answers.

+4  A: 

Observable state means the state of the object that can be observed by an external object. Internal caching only changes the private state of the object, but it doesn't make any observable difference to users of the object (except for non-functional matters like speed of access).

Skilldrick
@Skilldrick but can't I observed the area of this object? And what does it exactly mean "to observe"?
There is nothing we can do
It means to look at the object as an external observer. Is `area` private?
Skilldrick
@A-ha: If there was some method returning `area` or someting that is computed using `area` other objects could call that method and see that result now differs - that's how they would "observe" the change.
sharptooth
It depends on the implementation. If `-1` is being used to invalidate the area, then presumably any accessor function will check if `area` is `-1` and do something. We'd need more code to see exactly what's going on.
Skilldrick
+1  A: 

This means that state variable is not available for client code directly or through get function. From client point of view, class instance is not changed.

Alex Farber
+2  A: 

In this context, "observable state" means whether something is, directly or indirectly, visible to users of the object.
If using code can not, through observing the return value of any accessible member function or the value of any accessible data member, tell a difference between the object having one value of area or another, than area is not part of the object's observable state.
If, OTOH, using code could, for example, call a member function, and that function's result would differ depending on the value of area, then area would be part of the object's observable state.

sbi