Reputation: 393
is there any need for virtual when no inheritance is involved? I think that virtual function or keywork is tightly coupled with inheritance as per my level of understanding and knowledge. Am I right? Is there any place where virtual function comes to use other than with inheritance.(Base and derived classes)?
Upvotes: 3
Views: 96
Reputation: 96
Virtual is used only where runtime polymorphism is needed. Use of virtual make sure that correct version (BASE/DERIVED) of the method is getting called and the call is resolved at runtime according the type of caller object. For more refer to Virtual Functions
and YES, your understanding is correct.
Upvotes: 1
Reputation: 551
no, you are right, there is no use of virtual functions outside of inheritance, because virtual functions are specifically made in order to allow derived class to "override" base class functions (oftenly extending them by calling them and then do additional treatment)
Upvotes: 3
Reputation: 14392
Yes, you are right. Even more: virtual functions are only needed for runtime polymorphism, which is only a part of what inheritance is about.
Upvotes: 8