Polymorphism is based on dynamic binding and virtual functions.
Virtual function is a function whose behavior can be overridden within an inheriting class by a function with the same signature. The redefined the method from derived class can be accessed through a pointer or reference from base class. If virtual function called through reference or pointer to a base class, then this call will be resolved at run time and dynamic binding will happen.
Abstract class is a class that has one or more pure virtual member functions. You cannot make an object (instance) of an ABC
For all practical purposes, virtual functions aren't inlined.
That's because "inline" means "during compilation, replace the call site with the body of the called function," but "virtual" means "wait until runtime to see which function is called."
Virtual Table and Virtual Table Pointer
The implementation of virtual functions depends on "virtual table “.Each class in a program that declares or inherits virtual functions has its own vtbl, and the entries in a class's vtbl are pointers to the implementations of the virtual functions for that class. Each object of this class carries with it a hidden data member “virtual table pointer” that points to the virtual table for that class. When a virtual function is called, the actual function called is determined by following the object's vptr to a vtbl and then looking up the appropriate function pointer in the vtbl. Whenever there is a virtual function call, the v-table is used to resolve to the function address.
No comments:
Post a Comment