mirror of
https://github.com/pybind/pybind11.git
synced 2026-07-01 03:47:03 +00:00
general cleanup of the codebase
- new pybind11::base<> attribute to indicate a subclass relationship - unified infrastructure for parsing variadic arguments in class_ and cpp_function - use 'handle' and 'object' more consistently everywhere
This commit is contained in:
@@ -29,6 +29,11 @@ public:
|
||||
void bark() const { std::cout << "Woof!" << std::endl; }
|
||||
};
|
||||
|
||||
class Rabbit : public Pet {
|
||||
public:
|
||||
Rabbit(const std::string &name) : Pet(name, "parrot") {}
|
||||
};
|
||||
|
||||
void pet_print(const Pet &pet) {
|
||||
std::cout << pet.name() + " is a " + pet.species() << std::endl;
|
||||
}
|
||||
@@ -62,9 +67,14 @@ void init_ex5(py::module &m) {
|
||||
.def("name", &Pet::name)
|
||||
.def("species", &Pet::species);
|
||||
|
||||
/* One way of declaring a subclass relationship: reference parent's class_ object */
|
||||
py::class_<Dog>(m, "Dog", pet_class)
|
||||
.def(py::init<std::string>());
|
||||
|
||||
/* Another way of declaring a subclass relationship: reference parent's C++ type */
|
||||
py::class_<Rabbit>(m, "Rabbit", py::base<Pet>())
|
||||
.def(py::init<std::string>());
|
||||
|
||||
m.def("pet_print", pet_print);
|
||||
m.def("dog_bark", dog_bark);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user