Virtual functions when there is no inheritance

is there a need for virtual if inheritance is not involved? I think virtual function or manual work is closely related to inheritance according to my level of understanding and knowledge. I'm right? Is there any place where virtual function is used other than with inheritance. (Base and Derived Classes)?

+3


source to share


3 answers


Virtual is used only where runtime polymorphism is needed. Using a virtual machine ensures that the correct version (BASE / DERIVED) of the method is called and the call is resolved at runtime according to the type of the caller. See Virtual Functions for details



and YES, your understanding is correct.

+1


source


Yes you are right. What's more: virtual functions are only needed for runtime polymorphism , which is only part of what inheritance is.



+8


source


no, you are right, there are no virtual functions outside of inheritance, because virtual functions are specifically designed to allow a derived class to "override" the functions of the base class (often spreading them out, calling them, and then doing additional treatment)

+3


source







All Articles