I tried to bind a static function which returns a shared_ptr pointed to another class.
Here is the sample code
class Example {
public:
Example() {}
~Example() {}
};
class ABC {
public:
static std::shared_ptr<Example> get_example() {std::make_shared<Example();}
};
void init_abc(py::module & m) {
py::class_<Example>(m, "Example")
.def(py::init<>());
py::class_<ABC>(m, "ABC")
.def_static("get_example", &ABC::get_example);
}
Here is the python side
example = my_module.ABC.get_example()
However, python side threw a segmentation fault.
Any idea?
There are some missing bits in your code like >
. Here is working example:
class Example {
public:
Example() {}
~Example() {}
};
class ABC {
public:
static std::shared_ptr<Example> get_example() { return std::make_shared<Example>();}
};
Next, provide additional template argument shared_ptr<Example>
in place where you wrap Example
class:
py::class_<Example, std::shared_ptr<Example>>(m, "Example")
.def(py::init<>());
py::class_<ABC>(m, "ABC")
.def_static("get_example", &ABC::get_example);
This way shared_ptr<Example>
will be handled properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.