I understand the difference between static and dynamic binding in the sense that method calls are determined at compile time for static binding - whereas method calls are determined at run time for dynamic binding.
One thing I don't get is why you have to pass by reference or pointer for dynamic binding. I tried looking online but I am still confused. Is it because when you pass by value, you are passing a copy which means it has to be initialised which means it gets sliced?
For example, Pet
is a base class and Dog
is a derived class.
Now...
void print(Pet p) {} // Calls print from the pet class
void print(Pet &p) {} // Calls print from the type of pet object being passed. For example, Dog::print() rather than Pet::print()
If someone could explain this to me better it will really make me happy
Thanks