使用Pybind11并通过基础指针访问C++对象。

huangapple go评论69阅读模式
英文:

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

问题

以下是您要翻译的内容:

假设我有以下的C++类:

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>();
    }
}

是否可能,并且如果可能的话,如何使用Pybind11编写绑定,以便我可以在Python代码中编写:

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

并且正确调用派生类中的函数?

英文:

Suppose I have the following C++ classes:

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

class Dog : public Animal {
    public:
    void sound() override {
        std::cout &lt;&lt; &quot;Woof\n&quot;;
    }
};

class Cat : public Animal {
    public:
    void sound() override {
        std::cout &lt;&lt; &quot;Miao\n&quot;;
    }
};

std::unique_ptr&lt;Animal&gt; animalFactory(std::string_view type) {
    if (type == &quot;Dog&quot;) {
        return std::make_unique&lt;Dog&gt;();
    } else {
        return std::make_unique&lt;Cat&gt;();
    }
}

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

dog = animalFactory(&#39;dog&#39;)
cat = animalFactory(&#39;cat&#39;)
dog.sound()
cat.sound()

and have the correct functions in the derived classes called?

答案1

得分: 1

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);
}
import examples
dog = examples.animalFactory('Dog')
cat = examples.animalFactory('Cat')
dog.sound()
cat.sound()
英文:
PYBIND11_MODULE(examples, m) {

    py::class_&lt;Animal&gt;(m, &quot;Animal&quot;);

    py::class_&lt;Dog, Animal&gt;(m, &quot;Dog&quot;)
        .def(py::init())
        .def(&quot;sound&quot;, &amp;Dog::sound);

    py::class_&lt;Cat, Animal&gt;(m, &quot;Cat&quot;)
        .def(py::init())
        .def(&quot;sound&quot;, &amp;Cat::sound);

    m.def(&quot;animalFactory&quot;, &amp;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(&#39;Dog&#39;)
cat = examples.animalFactory(&#39;Cat&#39;)
dog.sound()
cat.sound()

huangapple
  • 本文由 发表于 2023年5月26日 16:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定