You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
void BindZoo(pybind11::module& m) {
pybind11::class_(m, "Zoo")
.def(pybind11::init<Animal*>())
}
when I use python like this
dog = Dog()
zoo = Zoo(dog)
it will assert type error, However in C++ Animal* is base class pointer, use it as parameter can accept derived class pointer and call the function go to realize polymorphism
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
class Animal {
public:
virtual ~Animal() { }
virtual std::string go(int n_times) = 0;
};
class Dog : public Animal {
public:
std::string go(int n_times) override {
std::string result;
for (int i=0; i<n_times; ++i)
result += "woof! ";
return result;
}
};
class Zoo {
public:
Animal* animal_=nullptr;
Zoo(Animal* animal) : animal_(animal){};
void call_go(){animal_->go};
}
void BindZoo(pybind11::module& m) {
pybind11::class_(m, "Zoo")
}
when I use python like this
dog = Dog()
zoo = Zoo(dog)
it will assert type error, However in C++ Animal* is base class pointer, use it as parameter can accept derived class pointer and call the function go to realize polymorphism
Beta Was this translation helpful? Give feedback.
All reactions