I started using std::exception
as a base class for all my exceptions recently. I could not properly override what()
without putting the virtual
keyword in front of it. Without the virtual
keyword it always seemed to call the what()
function of the base class, std::exception
.
It perplexed me a little because I thought that one never needed to put virtual
in front of a function when overriding it (and here is a post that seems to confirm that). But I decided to let it go and move on.
Then today while reading O'Reilly's "Safe C++" I found the author also overriding what()
with the virtual keyword. He wrote...
virtual const char* what() const throw () { /* stuff */ }
Why is he overriding a function and using the virtual
keyword? Is it just for "documentation" as suggested in the post I quoted above?