mutable and error flags
I have a class with a number of inspector functions, when a fail condition occurs in an inspector function, the class calls one of two private methods, reportWarning(s) and reportError(s). Both display a dialog, informing the user that an fail condition has been reached.
The difference is that when certain inspectors fail, no later inspectors should be allowed to succeed. These are the methods that call reportError, which then sets a bool m_bFatalError to true.
The problem is, that this means that reportError is non-const, so any inspectors that call it are non-const.
One way around this is to make m_bFatalError mutable. Is this a correct usage of mutable, or is it subversive? Is it the case that inspectors that may cause a fatal error are also conceptually non-const?
I read of exmaples of mutable members being things like a cache, to speed up lookups. In a way, this m_bFatalError is like a cache, caching the fact that you can''t read from this object any more.
Yes, that''s the intended usage of mutable - modifying members of const structures. Whether or not it''s good design is debatable. I guess in this case it is, although I think const-correctness isn''t worth all that much.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement