mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
Test and document binding protected member functions
This commit is contained in:
@@ -229,6 +229,57 @@ TEST_SUBMODULE(class_, m) {
|
||||
// This test is actually part of test_local_bindings (test_duplicate_local), but we need a
|
||||
// definition in a different compilation unit within the same module:
|
||||
bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local());
|
||||
|
||||
// test_bind_protected_functions
|
||||
class ProtectedA {
|
||||
protected:
|
||||
int foo() const { return value; }
|
||||
|
||||
private:
|
||||
int value = 42;
|
||||
};
|
||||
|
||||
class PublicistA : public ProtectedA {
|
||||
public:
|
||||
using ProtectedA::foo;
|
||||
};
|
||||
|
||||
py::class_<ProtectedA>(m, "ProtectedA")
|
||||
.def(py::init<>())
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1910
|
||||
.def("foo", &PublicistA::foo);
|
||||
#else
|
||||
.def("foo", static_cast<int (ProtectedA::*)() const>(&PublicistA::foo));
|
||||
#endif
|
||||
|
||||
class ProtectedB {
|
||||
public:
|
||||
virtual ~ProtectedB() = default;
|
||||
|
||||
protected:
|
||||
virtual int foo() const { return value; }
|
||||
|
||||
private:
|
||||
int value = 42;
|
||||
};
|
||||
|
||||
class TrampolineB : public ProtectedB {
|
||||
public:
|
||||
int foo() const override { PYBIND11_OVERLOAD(int, ProtectedB, foo, ); }
|
||||
};
|
||||
|
||||
class PublicistB : public ProtectedB {
|
||||
public:
|
||||
using ProtectedB::foo;
|
||||
};
|
||||
|
||||
py::class_<ProtectedB, TrampolineB>(m, "ProtectedB")
|
||||
.def(py::init<>())
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1910
|
||||
.def("foo", &PublicistB::foo);
|
||||
#else
|
||||
.def("foo", static_cast<int (ProtectedB::*)() const>(&PublicistB::foo));
|
||||
#endif
|
||||
}
|
||||
|
||||
template <int N> class BreaksBase { public: virtual ~BreaksBase() = default; };
|
||||
|
||||
Reference in New Issue
Block a user