In dynamic binding
, the function call is bound to the function implementation based on the type of object to which the pointer is pointing to.
Suppose we have the following code :
base *bptr = new derived;
bptr->func();
Let the function func
be declared virtual in the base class. Then the derived class's version of virtual function func
will be invoked at run-time due to dynamic binding.
I understand the above concept.
But i got confused by the following concept which i studied after studying the above concept.
In the above code snippet, a pointer to derived class object is implicitly converted to a pointer to base class object. Then bptr
will be actually pointing to the base class sub-object of derived class object and not pointing to the derived class object.
Since the base class pointer bptr
is pointing to base class sub-object, during run-time shouldn't the base class's version of virtual function func
be invoked?