I have the following code:
#include <iostream> #include "boost/shared_ptr.hpp" using boost::shared_ptr; class Base { public: virtual ~Base() {} virtual void print() = 0; }; template <typename T> class Child : public Base { public: virtual void print() { std::cout << "in Child" << std::endl; } }; class GrandChild : public Child<int> { public: virtual void print() { std::cout << "in GrandChild" << std::endl; } }; template <typename T> void call_print(shared_ptr<Child<T> > a) { a->print(); } void call_base_print(shared_ptr<Base> a) { a->print(); } int main() { shared_ptr<GrandChild> gchild(new GrandChild); call_print(shared_ptr<Child<int> >(gchild)); // call_print(gchild); // Cannot compile! call_base_print(gchild); // This works. return 0; }
It was strange to me that call_base_print(gchild) works, but call_print(gchild) causes a compiler error. I know that C ++ does not allow two implicit conversions, but I don't think there are two conversions here ... Can someone enlighten me?
source share