title: How to assign in c++ an object created in python?
body: <p>I've basically have a very simple node test case I'm trying to fix.</p>
  
  <p>I have a simple Node class with a getChild and a getParent</p>
  
  <p>The child can be assigned via the addChild function</p>
  
  <p>Then this function <strong>automatically set the parent on the relevant class (so from the c++ side)</strong></p>
  
  <p>Unfortunately when I do that I <strong>lose the python reference..</strong></p>
  
  <p>I guess the code should be better to understand :</p>
  
  <p>the <strong>basic main class MyNode</strong>:</p>
  
  <pre><code>class MyNode
  {
  public:
      MyNode(): m_name("unknown") {}
      MyNode(std::string name): m_name(name) {}
      MyNode(MyNode * const o) : m_name(o-&gt;m_name) {}
      virtual ~MyNode() {}
  
      std::string getName() const { return m_name; }
      void setName(std::string name) { m_name = name; }
      boost::shared_ptr&lt;MyNode&gt; getChild() const { return m_child; }
      const boost::shared_ptr&lt;MyNode&gt; &amp; getParent() const { return m_parent; }
  
      void addChild(boost::shared_ptr&lt;MyNode&gt; node) {
          m_child = node;
          node-&gt;m_parent = boost::make_shared&lt;MyNode&gt;(this);
      }
  
  private:
      std::string m_name;
      boost::shared_ptr&lt;MyNode&gt; m_parent;
      boost::shared_ptr&lt;MyNode&gt; m_child;
  };
  </code></pre>
  
  <p>Then the <strong>boost python bindings code</strong> :</p>
  
  <pre><code>class_&lt; MyNode, boost::shared_ptr&lt;MyNode&gt;/*, MyNodeWrapper*/ &gt;("MyNode", init&lt;std::string&gt;())
      .add_property( "Name", &amp;MyNode::getName, &amp;MyNode::setName )
      .add_property( "Child", &amp;MyNode::getChild )
      .add_property( "Parent", make_function(&amp;MyNode::getParent, return_internal_reference&lt;&gt;()))
      .def( "addChild", &amp;MyNode::addChild )
      ;
  </code></pre>
  
  <p>To finish <strong>my python test code</strong> :</p>
  
  <pre><code>&gt;&gt;&gt; p=MyNode("p")
  &gt;&gt;&gt; o=MyNode("o")
  &gt;&gt;&gt; p.addChild(o)
  &gt;&gt;&gt; o.Parent
  &lt;hello_ext.MyNode object at 0x01C055A8&gt;   &lt;&lt; this is not equal to p
  &gt;&gt;&gt; p
  &lt;hello_ext.MyNode object at 0x01C06510&gt;   &lt;&lt; as you can see p here
  &gt;&gt;&gt; o.Parent
  &lt;hello_ext.MyNode object at 0x01C055A8&gt;   &lt;&lt; but at least the pointer doesn't change each time the Parent is called !
  &gt;&gt;&gt; p.Child == o                          &lt;&lt; so for the child it works
  True
  &gt;&gt;&gt; o.Parent == p                         &lt;&lt; but it doeesn't work for Parent
  False
  </code></pre>
  
  <p>The issue is certainly in the addFunction and how I use boost::make_shared to set the parent.
  I unfortunately have no idea what's going on..
  I've tried to use a boost wrapper:</p>
  
  <pre><code>struct MyNodeWrapper : public MyNode, public boost::python::wrapper&lt;MyNode&gt;
  {
      MyNodeWrapper( PyObject * a_PyObj ) : m_Self( a_PyObj ) {}
      MyNodeWrapper( PyObject * a_PyObj, const MyNode &amp; a_Vec ) : MyNode( a_Vec ), m_Self( a_PyObj ) {}
      MyNodeWrapper( PyObject * a_PyObj, std::string name ) : MyNode( name ) , m_Self( a_PyObj ) {}
  
      PyObject * m_Self;
  };
  </code></pre>
  
  <p>But still I'm not sure how I should modify the addChild function</p>
  
  <p>Any idea ?</p>