Created
August 8, 2021 08:30
-
-
Save rajkosto/c3051b3dbc1f14be90b7179ce40ee98b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct DummyBase | |
{ | |
DummyBase() : val(5) {} | |
DummyBase(int newVal) : val(newVal) {} | |
void set_val(int newVal) { val = newVal; } | |
int get_val() const { return val; } | |
private: | |
int val; | |
}; | |
struct RealDummy : public DummyBase | |
{ | |
using DummyBase::DummyBase; | |
string to_string() const { return "MAGIC!"; } | |
}; | |
auto bcs = py::class_<DummyBase>(m,"DummyBase") | |
.def(py::init<>()) | |
.def(py::init<int>()) | |
.def("setVal",[](py::object srcObj, int newVal) { py::cast<DummyBase*>(srcObj)->set_val(newVal); return srcObj; }) | |
.def("getVal",[](const DummyBase& tgt) -> int { return tgt.get_val(); }); | |
auto dum = py::class_<RealDummy>(m,"RealDummy",bcs) | |
.def(py::init<>()) | |
.def(py::init<int>()) | |
.def("__str__",[](const RealDummy& tgt) { return tgt.to_string(); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment