title: Boost Python and shared_ptr upcast
body: <hr>
  
  <h2>Sample code for dll/pyd:</h2>
  
  <pre><code>#include &lt;boost/python.hpp&gt;
  #include &lt;iostream&gt;
  
  class Base
  {
  public: Base() {}
  public: int getValue() { return 1; }
  };
  
  typedef boost::shared_ptr&lt;Base&gt; BasePtr;
  
  class ParentA : public Base
  {   
  public: ParentA() : Base() {}
  };
  
  typedef boost::shared_ptr&lt;ParentA&gt; ParentAPtr;
  
  class Collector
  {
  public: Collector() {}
  public: void addParent(BasePtr&amp; parent)
  {
      std::cout &lt;&lt; parent-&gt;getValue() &lt;&lt; std::endl;
  }
  };
  
  typedef boost::shared_ptr&lt;Collector&gt; CollectorPtr;
  
  ParentAPtr createParentA()
  {
  return ParentAPtr(new ParentA());
  };
  
  BOOST_PYTHON_MODULE(hello)
  {
  boost::python::class_&lt;Base, BasePtr, boost::noncopyable&gt;("Base",
          boost::python::no_init)
          .def("getValue", &amp;Base::getValue)
  ;
  
  boost::python::class_&lt;ParentA, ParentAPtr, boost::python::bases&lt;Base&gt;&gt;("ParentA")
  ;
  
  boost::python::implicitly_convertible&lt; ParentAPtr, BasePtr &gt;();
  
  boost::python::def("createParentA", createParentA);
  
  boost::python::class_&lt;Collector, CollectorPtr&gt;("Collector")
      .def("addParent", &amp;Collector::addParent)
  ;
  }
  </code></pre>
  
  <hr>
  
  <h2>example code to test it in Python console:</h2>
  
  <pre><code>import hello
  p = hello.createParentA()
  c = hello.Collector()
  c.addParent(p)
  </code></pre>
  
  <hr>
  
  <h2>Initial post with fake code:</h2>
  
  <p>I'm having some problems in having Boost Python to upcast a shared_ptr from Python. </p>
  
  <pre><code>class Base() {...};
  class ParentA(): public Base {...};
  class ParentB(): public Base {...};
  
  typedef boost::shared_ptr&lt;Base&gt; BasePtr;
  typedef boost::shared_ptr&lt;Parent&gt; ParentPtr;
  
  class Collector() 
  {
    void addParent(BasePtr&amp; parent) {...}
  }
  typedef boost::shared_ptr&lt;Collector&gt; CollectorPtr;
  
  BOOST_PYTHON_MODULE(PythonModule)
  {
   boost::python::class_&lt;Collector, CollectorPtr&gt;("Collector")
    .def("addParent", &amp;Collector::addParent)
  
  
   boost::python::class_&lt;Base, BasePtr, boost::noncopyable&gt;("Base", 
              boost::python::no_init)
   ...
   ;
  
   boost::python::class_&lt;ParentA, ParentAPtr, 
              boost::python::bases&lt;Base&gt;&gt;("ParentA", 
          boost::python::init&lt;&gt;())
   ...
   ;
  
   boost::python::implicitly_convertible&lt; ParentAPtr, BasePtr &gt;();
  }
  </code></pre>
  
  <p>And in Python we do:</p>
  
  <pre><code>from PythonModule import *
  p = ParentA()
  c = Collector()
  c.addParent(p) # Fails here because no upcast is happening. 
  </code></pre>
  
  <p>Any ideas on how I can make this work?</p>
  
  <p>Compiled on VS2008, with BOOST 1.44 and Python 3.0.</p>
  
  <p>Thanks,</p>