[英]Polymorphism and pybind11
當我想在Python中使用C ++多態時,我對pybind11有一個奇怪的行為。 這是我的問題的一個簡單示例:
import polymorphism as plm
a = plm.mylist()
print(a)
a[0].print()
a[1].print()
這個腳本的輸出是
[MyBase,MyDerived]
MyBase
MyBase
但預期的產出是
[MyBase,MyDerived]
MyBase
MyDerived
因為mylist返回一個std :: vector,它包含一個派生類(MyDerived)的實例作為第二個成員。 奇怪的是,當我打印整個列表時,MyDerived被識別出來。
這是C ++代碼的頭文件:
/* polymorphism.hpp */
#ifndef POLYMORPHISM_HPP
#define POLYMORPHISM_HPP
#include <vector>
class MyBase
{
public:
virtual void print() const;
};
class MyDerived : public MyBase
{
public:
virtual void print() const;
};
std::vector<MyBase*> mylist();
#endif
這是cpp文件:
#include "polymorphism.hpp"
#include <iostream>
#include <pybind11/stl.h>
#include <pybind11/pybind11.h>
void MyBase::print() const
{ std::cout << "MyBase" << std::endl; }
void MyDerived::print() const
{ std::cout << "MyDerived" << std::endl; }
std::vector<MyBase*> mylist()
{
std::vector<MyBase*> list(2);
list[0] = new MyBase();
list[1] = new MyDerived();
return list;
}
PYBIND11_MODULE(polymorphism, m)
{
pybind11::class_<MyBase>(m, "MyBase")
.def(pybind11::init<>())
.def("print", &MyBase::print)
.def("__repr__", [](const MyBase &a) { return "MyBase"; });
pybind11::class_<MyDerived, MyBase>(m, "MyDerived")
.def(pybind11::init<>())
.def("print", &MyDerived::print)
.def("__repr__", [](const MyDerived &a) { return "MyDerived"; });
m.def("mylist", &mylist, "return a list");
}
編輯:更令人驚訝的是,當我刪除MyDerived的“打印”綁定時,我收到以下錯誤消息
[MyBase,MyDerived]
MyBase
Traceback(最近一次調用最后一次):
文件“test.py”,第8行,in
一個[1] .PRINT()
AttributeError:'polymorphism.MyDerived'對象沒有屬性'print'
此消息似乎意味着MyDerived在調用錯誤版本的打印時得到了充分認可(如果我理解的話)。
編輯2:這是一個使用蹦床類的版本。 但是,此版本導致相同的錯誤輸出。
/* polymorphism.hpp */
#ifndef POLYMORPHISM_HPP
#define POLYMORPHISM_HPP
#include <vector>
#include <pybind11/stl.h>
#include <pybind11/pybind11.h>
class MyBase
{
public:
virtual void print() const;
};
class MyDerived : public MyBase
{
public:
virtual void print() const;
};
std::vector<MyBase*> mylist();
class PyMyBase : public MyBase
{
public:
using MyBase::MyBase; // Inherit constructors
void print() const override { PYBIND11_OVERLOAD(void, MyBase, print ); }
};
class PyMyDerived : public MyDerived
{
public:
using MyDerived::MyDerived; // Inherit constructors
void print() const override { PYBIND11_OVERLOAD(void, MyDerived, print);}
};
#endif
這是相應的cpp文件:
/* polymorphism.cpp */
#include "polymorphism.hpp"
#include <iostream>
void MyBase::print() const
{ std::cout << "MyBase" << std::endl; }
void MyDerived::print() const
{ std::cout << "MyDerived" << std::endl; }
std::vector<MyBase*> mylist()
{
std::vector<MyBase*> list(2);
list[0] = new MyBase();
list[1] = new MyDerived();
return list;
}
PYBIND11_MODULE(polymorphism, m)
{
pybind11::class_<MyBase, PyMyBase>(m, "MyBase")
.def(pybind11::init<>())
.def("print", &MyBase::print)
.def("__repr__", [](const MyBase &a) { return "MyBase"; });
pybind11::class_<MyDerived, PyMyDerived>(m, "MyDerived")
.def(pybind11::init<>())
.def("print", &MyDerived::print)
.def("__repr__", [](const MyDerived &a) { return "MyDerived"; });
m.def("mylist", &mylist, "return a list");
}
我不知道為什么,但是pybind11
似乎在mylist()
的原始指針有問題。 如果將返回類型更改為vector<unique_ptr<MyBase>>
則該示例可正常工作。 以下示例編譯為python模塊example
並生成預期輸出。
example.cpp:
#include <pybind11/stl.h>
#include <pybind11/pybind11.h>
#include <iostream>
#include <memory>
#include <vector>
class MyBase {
public:
virtual void print() const {
std::cout << "MyBase::print()" << std::endl;
}
};
class MyDerived : public MyBase {
public:
virtual void print() const override {
std::cout << "MyDerived::print()" << std::endl;
}
};
std::vector<std::unique_ptr<MyBase>> mylist() {
std::vector<std::unique_ptr<MyBase>> v;
v.push_back(std::make_unique<MyBase>());
v.push_back(std::make_unique<MyDerived>());
return v;
}
PYBIND11_MODULE(example, m) {
pybind11::class_<MyBase>(m, "MyBase")
.def(pybind11::init<>())
.def("print", &MyBase::print)
.def("__repr__", [](MyBase const&) { return "MyBase"; });
pybind11::class_<MyDerived>(m, "MyDerived")
.def(pybind11::init<>())
.def("print", &MyDerived::print)
.def("__repr__", [](MyDerived const&) { return "MyDerived"; });
m.def("mylist", &mylist, "returns a list");
}
python shell:
>>> import example
>>> l = example.mylist()
>>> l[0].print()
MyBase::print()
>>> l[1].print()
MyDerived::print()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.