-
I have a binding cpp as follow: #include <pybind11/pybind11.h>
#include <iostream>
namespace py = pybind11;
class Pet
{
public:
Pet(const std::string &name) : name(name) { }
//virtual void blah() {}
std::string name;
};
class Dog : public Pet
{
public:
Dog(const std::string &name) : Pet(name) { }
void bark() { std::cout << "Woof!\n"; }
};
PYBIND11_MODULE(pcourse, m)
{
m.doc() = "pybind11 Course with examples";
// binding a class
py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &>())
.def_readwrite("name", &Pet::name)
;
// Inheritance template parameter
py::class_<Dog, Pet>(m, "Dog")
.def(py::init<const std::string &>())
.def("bark", &Dog::bark)
;
// Return a base pointer to a derived instance
m.def("pet_store", []() { return std::unique_ptr<Pet>(new Dog("Bijou")); });
} When I run the Python code: import pcourse as example
pet = example.pet_store()
print(pet)
print(type(pet))
pet.bark() as expected, I get a "AttributeError: 'pcourse.Pet' object has no attribute 'bark'" error But when I uncomment "virtual void blah() {}" and build the module, I get no error and it prints "Woof!" It should not do that because it is a base class. Anyone know why ? Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
faalbers
Sep 4, 2021
Replies: 1 comment
-
Never mind !! The manual explains why ! Duh !!!
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
faalbers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never mind !! The manual explains why ! Duh !!!