A common error in C++ that the compiler cannot catch for you, is getting a base function called instead of the expected overridden one because you forgot to make it virtual. Example:
#include <iostream> using namespace std; struct Base { void print() { cout << "Base" << endl; } }; struct Derived : public Base { void print() { cout << "Derived" << endl; } }; int main() { Base b; Derived d; b.print(); //Base d.print(); //Derived Base *bp = &d; bp->print(); //Base (Here we would like to see Derived) }
In Java, this would print Derived, as methods are virtual by default. In C++ however, this is not so. But if we add virtual
to the declaration, we get the result we are looking for:
(...) struct Base { virtual void print() { (...) b.print(); //Base d.print(); //Derived bp->print(); //Derived
Why is this so? Why can’t all member functions be virtual, just like in most other object oriented languages? There are a couple of reasons:
Do not pay for what you do not need
There is an important guideline in the design of C++, that a feature should not incur any overhead when it is not used. And polymorphism is impossible to achieve without at least a small overhead. The way this is usually implemented is with a table of function pointers for each class, called a virtual table, or just vtbl. In our example, Base
and Derived
would each have a table with one entry, pointing to Base::print()
and Derived::print()
, respectively. Note that there is only one vtbl per class, not per object, so this overhead is not large, but it is there.
In addition to a vtbl per class, every object has a pointer to the vtbl of its class, adding (usually) one word to each object. This is another small spacial overhead.
Finally, there is a small overhead in time as well, as functions get an extra level of indirection as they are resolved through the vtbl. This is however usually negligible.
All in all, the overhead is so small that one might challenge the non-virtual default. But still, a rule is a rule. And there is more!
Backward compatibility
C++ was designed to be as close to 100% compatible with C as possible. If you have an object of a really simple class*, you can pass it to a C function just as if it were an instance of a good old C struct, and be sure that it will be ok. Its size is just the combined size of its data members (plus padding, if any), and it can be copied with memcpy()
. As soon as you add a virtual method, the size of the object is no longer the size expected by C, it has an extra magic word (or maybe more) that C won’t recognize, and it cannot be portably used with memcpy()
.
And a third one?
There is a third argument as well, arguing that virtual methods break encapsulation, and should be used with care. While I agree, other languages have solved this in a perfectly good manner, by having member functions virtual by default, but making it possible to disallow inheritance. Java does this by the final
keyword. Which is why I don’t really count this as an argument against virtual-by-default.
So if you don’t want to pay for what you don’t use, and if you depend on C, you cannot have virtual-by-default.
* More specifically a Plain Old Data Structure (POD), which I won’t cover here.