Using Pybind11 and access C++ objects through a base pointer

57 views Asked by At

Suppose I have the following C++ classes:

class Animal {
    public:
    virtual void sound() = 0;
};

class Dog : public Animal {
    public:
    void sound() override {
        std::cout << "Woof\n";
    }
};

class Cat : public Animal {
    public:
    void sound() override {
        std::cout << "Miao\n";
    }
};

std::unique_ptr<Animal> animalFactory(std::string_view type) {
    if (type == "Dog") {
        return std::make_unique<Dog>();
    } else {
        return std::make_unique<Cat>();
    }
}

Is it possible, and if so, how, to write a binding using Pybind11 so that I in Python code can write:

dog = animalFactory('dog')
cat = animalFactory('cat')
dog.sound()
cat.sound()

and have the correct functions in the derived classes called?

1

There are 1 answers

0
HarryP2023 On
PYBIND11_MODULE(examples, m) {

    py::class_<Animal>(m, "Animal");

    py::class_<Dog, Animal>(m, "Dog")
        .def(py::init())
        .def("sound", &Dog::sound);

    py::class_<Cat, Animal>(m, "Cat")
        .def(py::init())
        .def("sound", &Cat::sound);

    m.def("animalFactory", &animalFactory);
}

I'd recommend reading the docs here: https://pybind11.readthedocs.io/en/stable/advanced/classes.html

Note that with my example, you will need to write the following in python:

import examples
dog = examples.animalFactory('Dog')
cat = examples.animalFactory('Cat')
dog.sound()
cat.sound()