Polymorphic dispatch is done via a base pointer or reference type, it does not cast to derived pointer/reference type automatically.
std::vector<boost::shared_ptr<Fruit> > basket;
boost::shared_ptr<Apple> p_apple(new Apple);
basket.push_back(p_apple);
When you call push_back
, the objects are stored as boost::shared_ptr<Fruit>
not boost::shared_ptr<Apple>
They can only be called by
void Print_via_ptr(const boost::shared_ptr<Fruit> p_a);
^^^^^
Same issue with your raw pointer functions.
Or you could use template to generate calls for you, but this maybe is not your purpose.
template<typename T>
void Print_via_ptr(T p_o)
{
std::cout << p_o->who_am_i() << std::endl;
}