我试图让所以这段代码返回相同的实例中都初始化函数和回调函数升压蟒蛇返回同一个实例与make_constructor
test1.py
import test1
c = test1.C()
print 'init:', c
def func(c):
print 'func:', c
test1.register_callback(func)
test1.cpp
#include <iostream>
#include <vector>
#include <boost/python.hpp>
using namespace boost::python;
class C;
std::vector<boost::shared_ptr<C>> v;
class C
: public boost::noncopyable
{
public:
C() {
std::cout << "C()" << std::endl;
}
~C() {
std::cout << "~C()" << std::endl;
}
};
boost::shared_ptr<C> create() {
C *c = new C();
auto ptr = boost::shared_ptr<C>(c);
v.push_back(ptr);
return ptr;
}
void register_callback(object func) {
func(v[0]);
}
BOOST_PYTHON_MODULE(test1)
{
class_<C, boost::shared_ptr<C>, boost::noncopyable>("C", no_init)
.def("__init__", make_constructor(&create))
;
def("register_callback", register_callback);
}
我现在得到的输出是:
init: <test1.C object at 0x7f62181bd5d0>
func: <test1.C object at 0x7f62181c1848>
什么我想要得到的是:
init: <test1.C object at 0x7f62181bd5d0>
func: <test1.C object at 0x7f62181bd5d0>
这是可能的,如何?
这是正确的方法,谢谢! – Markus