Warning: Purely technical.
This is from a series of articles I had written for a journal.
The only thing constant in this world is change. The truth of this statement hurts.
Today Tom was in a particularly bad mood, "Think outside the box" he said. Upon a little bit of enquiry at the coffee room, I knew what the problem was. He had a meeting with our director and was asked about the objectives of our project. The screw-up that he was, Tom had blurted out some incongruent rationale, which had been, received none too kindly by the few who were listening. The net result was that Tom himself was now not sure as to what we were trying to accomplish.
Kevin was peering across a piece of code on my machine when Tom came across us.
Void SomeAccessor( const Base_t& aBaseClass ) const;
Void SomeOtherAccessor( Base_t& aBaseClass );
The compiler was happily flashing an error message. "Unfortunately, we can’t make SomeOtherAccessor a const function. That code is owned by the core group and we don’t even have access to the source code". Kevin continued "I guess we can try casting away the constness of theBaseClass and see whether SomeOtherAccessor is actually a const function. "Think outside the box, guys, cast the const away" Tom blurted out.
"There is one small problem with this solution" I started "According to the standard, the behavior if a function tries to modify an object whose const has been cast away is undefined! And secondly, a const_cast, contrary to appearances, cannot cast away the constness of an object that is actually defined const. I.e aBaseClass refers to theBaseClass which has been defined const. Had it been non const, it would be possible to cast away the constness of aBaseClass. The solution is to make theBaseClass a non const."
Realizing that he couldn’t make any meaningful contribution to the discussion, Tom started walking away "Think outside the box, boys, just get it done".
"That is very interesting" Kevin said, ignoring Tom. "I had the idea that you could use const_cast to cast away the constness of any object. How silly would I have looked had a tried to do something like this- const int &k; int j = const_cast." I always had a feeling that Kevin felt that his compiler was all too human, my doubts were augmented by this comment about his compiler judging his competence.
I looked out of my window and smiled. There was a constant in life. An object I declare to be const, come rain or shine, it will remain the same!