Why virtual, why not

Using a non-virtual method in C++ is syntactic sugar:  It doesn’t allow you to do anything you could not as easily do BY using a static member function and passing in the object instance as the first parameter.  If you don’t fully qualify the name of the function, the type checking of the first parameter will make sure you call the right version, or at least get a compilation error until you do fully qualify.The only reason you would need to use one is for template programming, where some classes have the method you wish to call as a virtual, and you want to be consistent across all classes passed to the template.

This is not to say that all non-virtual functions are bad, just an observation about what the language  provides.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.