tags:

views:

168

answers:

8

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.

A: 

If everything is going to be overriden, why not implement this as an interface?

Karim
This would be great advice, if C++ had interfaces.
anon
And even then, it wouldn't really apply here. The OP does mention that the base class implements some basic functionality.
Adrian Grigore
Depending on the target compiler, interfaces are not available as a native option. For example, in Borland C++ Builder 6.0, interfaces are mapped to abstract classes by using simple define's.
Juliano
Just because there's no formal language feature called "interface" doesn't make this a bad answer. The general term "interface" certainly applies to an abstract base class with nothing but pure virtual functions.
Cogwheel - Matthew Orlando
+5  A: 

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.

DeadMG
Well I can't foresee my needing runtime polymorphism in my own applications anytime soon, but I Can imagine very legitimate uses for my library that would. How significant is the performance hit?
Alex
Depends on the use case, however they're not substantially slower than regular function calls. It can be just a couple of ops.
DeadMG
+2  A: 

Any method that you want to be override-able should be declared virtual.

The advantage of a virtual method is that in this instance:

BaseClass * o = new SubClass;
o->MyVirtualMethod();

... it is SubClass::MyVirtualMethod() that will be called (which is typically what you want, if SubClass defined a MyVirtualMethod). If the method was non-virtual, then BaseClass::MyVirtualMethod() would be called instead, because the pointer (o) is of type BaseClass.

The disadvantage of a virtual method is that it is slightly more expensive (in terms of CPU cycles used) to call than a non-virtual method.

Jeremy Friesner
+2  A: 

The answer is already part of your opening post - You already said that every method is going to be overridden. Simply make all functions which should be overrideable virtual and leave the rest non-virtual.

Adrian Grigore
+1  A: 

This is called interface programming - there's nothing wrong with it, and in fact, it's often a great idea!

BlueRaja - Danny Pflughoeft
+3  A: 

Using virtual is about being able to use derived class code having only the abstract class type in hand.
If you always have the exact type of your object in hand, you don't need virtual, and your abstract class is useless anyway.
If you want to use your objects through Abstract&. Use virtual function member.
You may want to read this.

(... since when interfaces exist in standard c++ ?)

If you really really really want interface: Use the boost interface library

Ugo
Well, not strictly an interface, but rather a header file that defines my abstract superclass.
Alex
It is actually a very good idea but c++ is disappointing on this point. One "easy" way to force derived class to implement functionalities is to template the abstract class with the exact type E of the derived class and then make dummy method calls on E in the base class constructor. Take a look at Bil if you want to learn more about this.
Ugo
"interface" had a general meaning before languages started building in formal features called "interface." I don't understand why people are so stuck up about referring to abstract base classes with nothing but pure virtual functions in C++ as "interfaces"
Cogwheel - Matthew Orlando
Well you are right ! But "Simply use interfaces !!! :)" is still a wrong answer here. Moreover C++ "interfaces" are bad interfaces since they don't complain at compile time...
Ugo
A: 

Make as few methods virtual as will do the job. Try to consider aggregation over inheritance.

Space_C0wb0y
+1  A: 

Virtual functions let you call a method on a base class pointer and get the correct class' behavior, without knowing whether the object is of the base or derived class. That is the "advantage" of virtual functions, though really it's the very essence, and the very thing they were designed for.

So, whether you need this feature or not should be plainly and clearly evident from the design and intended use of your class. It shouldn't be a matter of "hmm, what are the tradeoffs", but rather "Without virtual functions, the implementation will be a complicated mess."

Given that, there are run-time costs to virtual functions, but that should never be a factor in determining whether to use them or not; the costs are trivial compared to the advantage. Rather, the option of having non-virtual functions in C++ is there so that if you know you don't need them, you don't have even that small overhead. In Java, all functions are virtual; there is no option to not have it, the reasoning being even if you don't need it, you get it anyway, it hardly costs anything anyway compared to the runtime overhead of everything else in the language.

Paul Richter