C++ Virtues of a virtual override

I've fallen into the habit of attaching the override modifier to virtual methods in derived classes.

There's a very good reason for this.

Years ago, I added a parameter to a virual method in a base class. I edited dozens of header and source files to match. Unknown to me, I had missed one. It compiled. It ran. But now there was code calling the base class method instead of the derived method. It took a team of eight a full week to find my mistake.

With the override keyword, that would have been a compile error. Something instantly identified and fixed.
 
And auto conversion of parameters makes figuring out which function is called into a wonderful intellectual puzzle. But I don't get paid to solve intellectual puzzle.

For a while, we had a coding rule that any single-argument constructor has to have a second parameter called "dummy", to prevent auto-conversion, and that conversion operators were forbidden. Then the "explicit" keyword came out, and I spent a week removing all those second parameters.
 
Back
Top