boostorg / boostorg/python

Base class of non exposed derived class in Python side?

未关闭
#415 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C++
星标
537
派生
223
平均合并
11 小时 22 分钟
30 天内合并 PR
2

描述

Hello, I have a use case where a polymorphism factory can instantiate object of a non exposed class. I have a multi-level inheritance scheme and the last level is not exposed to Python. What I try to understand is why the Python object of the last level takes the type of the top-most class in the inheritance hierarchy and not the one just above?

Here is a minimal example to reproduce the behavior:

C++ side
```
class Base
{
public:
Base() {}
virtual ~Base() = default;
};

class Derived: public Base
{
public:
Derived(int value) { m_value = value; }
~Derived() = default;

int getValue() { return m_value; }

private:
int m_value;
};

class DerivedTwo: public Derived
{
public:
DerivedTwo(int value): Derived(value) {}
~DerivedTwo() = default;
};

std::shared_ptr factory()
{
return std::make_shared(9);
}

std::shared_ptr factoryTwo()
{
return std::make_shared(99);
}

BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;

class_, boost::noncopyable>("Base", init<>());

class_, std::shared_ptr, boost::noncopyable>("Derived", init())
.def("get_value", &Derived::getValue)
;

def("factory", &factory);
def("factory_two", &factoryTwo);
}
```
Python side
```
import mymodule

obj = mymodule.factory()
print(type(obj))

obj2 = mymodule.factory_two()
print(type(obj2))
```
Output

```

<-- WHY?
```
Obviously, if I expose DerivedTwo class, all is OK.
Thanks for your help.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。