can dynamic binding happen without using pointer and using regular object in c++

For the call expression d1.fun() to work using dynamic binding both of the below given conditions must be satisfied:

  1. The call should be made using a reference or a pointer to the Base class.
  2. 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
    
}

Leave a Comment