Search code examples
c++virtual

Just how much do I want to make virtual?


I am writing an abstract superclass where literally every method is going to be overridden. There is some default functionality I could implement, but most of the time it's enough to leave the implementation to the subclass writer.

Since just about every method is going to be overwritten, how much should I make virtual and how much should I just leave as regular methods? In the current incarnation, everything is virtual, but I still haven't let this loose to anyone to use, so the design is flexible.

What advantages/disadvantages are there to virtual functions? Links to good reading material about this would be appreciated.


Solution

  • Virtual function calls are slower than non-virtual ones, but if you need runtime polymorphism, there's no alternative. Virtual function calls have a roughly constant overhead regardless of how many you have, so there's little disadvantage. You can see the design of many professional classes- like IDirect3D9Device- have many, many virtual methods in one class.