For the call expression d1.fun()
to work using dynamic binding both of the below given conditions must be satisfied:
- The call should be made using a reference or a pointer to the Base class.
- The member function
fun
should be a virtual member function.
If any of the above 2 conditions is not met, then we will have static binding.
Since in your example, d1
is an ordinary(non-reference/ non-pointer) object, condition 1 is violated(not met), and so we have static binding.
int main(){
Derived d1;
Base *bPtr = &d1;
bPtr->fun(); //dynamic binding
Base &bRef = d1;
bRef.fun(); //dynamic binding
d1.fun(); //static binding
}