This is called slicing.
When A passed by value, a local copy is created by copying only part A of the calling object. Since it has both static and dynamic type A , an override of print is selected.
Passing by reference, this function receives a reference to the calling object with dynamic type B , so an override is selected.
The use of abstract base classes can prevent the confusion of such behavior - they cannot be directly created and therefore cannot be passed by value.
Mike seymour
source share