[C++-sig] a boost.python question about parameter that is a pointer of instance.

simon zhang hapopen at gmail.com
Sat Nov 17 16:25:59 CET 2012


This is a complex problem. I provide some code.

c++
=======================================
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

class Rectangle {
public:
    Rectangle();
    virtual ~Rectangle();
    void set(int lg,int wd) { long=lg; width=wd; };
    int area() { return long*width; } ;
    int long;
    int width;
}

void runtest( boost::python::object &func ) {
    Rectangle rt;
    rt.set(2,3);
    func( &rt );          // The parameter is a pointer of instance.The
function come from python.
}

BOOST_PYTHON_MODULE(ctopython) {
    using namespace boost::python;
    def("runtest",runtest);
}

python
=======================================
import ctopython

def pfunc( ret )
    print ret.long                   #how to get right from a C++ instance
    print ret.area()

ctopython.runtest( pfunc )    #Pass parameters that is custom python object
to c++ function

=======================================
how to do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20121117/56b7e906/attachment.html>


More information about the Cplusplus-sig mailing list