Why C++ Virtual Functions Are Necessary


I came across the topic of virtual functions, but did not quite understand why they are needed and how to use them correctly, why a virtual destructor is needed, etc.


As for the virtual destructor, it is always needed if there is at least one virtual function, otherwise all destructors will not be called.

Why are virtual functions needed? To ensure polymorphism – one of the basic OOP whales, so that by reference / pointer to an object not to determine its type independently using if's, but to let the compiler and / or runtime do it. You can also read Wikipedia.

Scroll to Top