Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Why does a struct\class need a virtual method in order to be polymorphic?

Following this question, I'm wondering why a struct\class in C++ has to have a virtual method in order to be polymorphic.

Forcing a virtual destructor makes sense, but if there's no destructor at all, why is it mandatory to have a virtual method?

like image 971
Jonathan Livni Avatar asked Apr 29 '11 12:04

Jonathan Livni


People also ask

What is the necessity of virtual function in polymorphism?

Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime polymorphism. Functions are declared with a virtual keyword in base class. The resolving of function call is done at runtime.

What makes a class polymorphic?

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class.

Why do we need virtual function?

A virtual function in C++ helps ensure you call the correct function via a reference or pointer. The C++ programming language allows you only to use a single pointer to refer to all the derived class objects.

What is the purpose of marking a function as virtual in a base class?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.

What is required for polymorphism in C++?

Polymorphism in C++ Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.


2 Answers

Because the type of a polymorphic object in C++ is, basically, determined from the pointer to its vtable, which is the table of virtual functions. The vtable is, however, only created if there's at least one virtual method. Why? Because in C++, you never get what you didn't explicitly ask for. They call it "you don't have to pay for something you don't need". Don't need polymorphism? You just saved a vtable.

like image 65
OregonGhost Avatar answered Oct 31 '22 00:10

OregonGhost


Forcing a virtual destructor makes sense

Exactly. To destruct a virtual class manually (via delete) through its base class you need a virtual destructor. (Now, as I’ve been reminded in the comments, this isn’t usually needed: rather than use manual memory management, one would rely on modern smart pointers which also work correctly with non-virtual destructors.)

So any class which acts as a polymorphic base class usually needs either a virtual destructor or virtual functions anyway.

And since having runtime polymorphism adds an overhead (the class needs to store an additional pointer to its virtual method table), the default is not to add it, unless necessary anyway: C++’ design philosophy is “you only pay for what you need”. Making every class have a virtual method table would run afoul of this principle.

like image 21
Konrad Rudolph Avatar answered Oct 31 '22 00:10

Konrad Rudolph