Friday, October 17, 2008

C++ mutable, finally makes sense

After being a long time basher of C++'s mutable type specifiers, I have finally come to embrace this controversial concept (but its still in beta state :-)

This happened while implementing the FixSession class for my FIXTransit project. I was passing FixSession objects as constant references to other classes, but realized that this wouldn't be possible because some logically-read-only functions might end up modifying the SessionState data member. For example Session::createHeader() whose job is to simply return a FIX message Header for that Session, would result in incrementing the SessionState::seqNum property.

So now that the rationale behind the divine concept had occured to me, I very proudly declared the SessionState data member as mutable and continued to pass the Session objects as logically constant references :-)

Now I might end up in revisiting and changing this design altogether, however I feel that I have learned an important lesson from this. Also I would still maintain that this is not an ideal approach and should be adopted in very rare situations.