I'm writing an abstract superclass where literally every method will probably be overridden. There's some default functionality I possibly could implement, but more often than not it's enough to depart the implementation towards the subclass author.
Since nearly every method will probably be overwritten, just how much must i make virtual and just how much must i just leave as regular techniques? In the present version, things are virtual, however i still haven't permit this to loose to anybody to make use of, therefore the design is flexible.
What advantages/disadvantages exist to virtual functions? Links to get affordable reading through material about this is appreciated.
Virtual function calls are reduced than non-virtual ones, but when you'll need runtime polymorphism, there is no alternative. Virtual function calls possess a roughly constant overhead no matter the number of you've, so there's little disadvantage. You can observe the style of many professional classes- like IDirect3D9Device- have numerous, many virtual techniques in a single class.
Using virtual is all about having the ability to use derived class code getting just the abstract class key in hands.
If a person always has the precise kind of your object in hands, you do not need virtual, as well as your abstract class is useless anyway.
If you wish to make use of your objects through Abstract&lifier. Use virtual function member.
You might want to read this.
(... for how long connects appear in standard c++ ?)
Should you really really want interface: Make use of the boost interface library
Any method that you would like to become override-able ought to be declared virtual.
The benefit of an online technique is that in cases like this:
BaseClass * o = new SubClass; o->MyVirtualMethod();
... it's SubClass::MyVirtualMethod() that'll be known as (that is typically what you would like, if SubClass defined a MyVirtualMethod). When the method was non-virtual, then BaseClass::MyVirtualMethod() could be known as rather, since the pointer (o) is of type BaseClass.
The drawback to an online method is it is a little more costly (when it comes to CPU cycles used) to call than the usual non-virtual method.
The reply is already a part of your opening publish - You already stated that each method will probably be overridden. Just make all functions that ought to be overrideable virtual and then leave the relaxation non-virtual.
This really is known as interface programming - there is nothing wrong by using it, and actually, it's frequently a good idea!
Virtual functions allow you to call a technique on the base class pointer and obtain the right class' behavior, not understanding if the object is from the base or derived class. That's the "advantage" of virtual functions, though really it is the very essence, and also the very factor these were created for.
So, whether you'll need this feature or otherwise ought to be plainly and clearly apparent in the design and intended utilization of your class. It should not be dependent on "hmm, do you know the tradeoffs", but instead "Without virtual functions, the implementation is a complicated mess."
Considering that, you will find run-time costs to virtual functions, but that will not be an issue in identifying whether for their services or otherwise the expense are trivial in comparison towards the advantage. Rather, a choice of getting non-virtual functions in C++ can there be to ensure that knowing you do not need them, you do not have even that small overhead. In Java, all functions are virtual there's no choice to not have access to it, the reasoning being even when you do not need it, you receive it anyway, it hardly costs anything anyway in comparison towards the runtime overhead of anything else within the language.
If everything will probably be overriden, why don't you implement this being an interface?
Make as couple of techniques virtual as is going to do the task. Attempt to consider aggregation over inheritance.