title: Boost-python How to pass a c++ class instance to a python class
body: <p>I am new to boost python. I have to first init a cpp class instance in cpp code, and then pass this cpp instance to python code, use a python class instance to invoke it(the cpp instance). I have tried the Python/C API way, but failed, so I wonder how to pass a c++ class instance to a python class. </p>
  
  <p>The following is my code, changed from the boost python demo.</p>
  
  <p>in main.cpp</p>
  
  <pre><code>#include &lt;python2.6/Python.h&gt;
  #include &lt;boost/python.hpp&gt;
  #include &lt;iostream&gt;
  
  using namespace boost::python;
  using namespace std;
  
  class World
  {
  private:
      string name;
  public:
      void set(string name)
      {
          this-&gt;name = name;
      }
      void greet()
      {
          cout &lt;&lt; "hello, I am " &lt;&lt; name &lt;&lt; endl;
      }
  };
  
  typedef boost::shared_ptr&lt; World &gt; world_ptr;
  
  BOOST_PYTHON_MODULE(hello)
  {
      class_&lt;World&gt;("World")
      .def("greet", &amp;World::greet)
      .def("set", &amp;World::set)
      ;
  
      register_ptr_to_python&lt;world_ptr&gt;();
  };
  
  int main()
  {
      Py_Initialize();
      PyRun_SimpleString("import sys");
      PyRun_SimpleString("sys.path.append('./')");
  
      world_ptr worldObjectPtr (new World);
      worldObjectPtr-&gt;set("C++!");
  
      try
      {
          inithello();
          PyObject* pModule =PyImport_ImportModule("python");
          PyObject* pDict = PyModule_GetDict(pModule);
          PyObject* pClassHelloPython = PyDict_GetItemString(pDict, "Person");
          PyObject* pInstanceHelloPython = PyInstance_New(pClassHelloPython, NULL, NULL);
  
          PyObject_CallMethod(pInstanceHelloPython, "sayHi", NULL);
          worldObjectPtr-&gt;greet();
          PyObject_CallMethod(pInstanceHelloPython, "greetReset", "O", worldObjectPtr);
          worldObjectPtr-&gt;greet();
      }
      catch (error_already_set)
      {
          PyErr_Print();
      }
  
      Py_Finalize();
  
      return 0;
  }
  </code></pre>
  
  <p>in python.py</p>
  
  <pre><code>class Person:
      def sayHi(self):
          print 'hello from python'
  
      def greetReset(self, instance):
          instance.set('Python')
  </code></pre>
  
  <p>In the above code, I want to pass the worldObjectPtr to the pInstanceHelloPython, thus, pInstanceHelloPython can set the worldObjectPtr->name to Python. But I just don't know how to do it. Thank you for your patience in advance!!</p>