Base class of non exposed derived class in Python side?
まだ誰も着手していません。
評価
調査の方向性
示されている Base、Derived、DerivedTwo、factory、factoryTwo の定義を使用して、例を再現します。まず Boost.Python の class_ 登録、bases 宣言、shared_ptr の変換動作を読み、DerivedTwo を登録した場合と登録しない場合で、返される Python 型を比較します。型の選択動作が説明され、fix の想定される扱いまたは対象範囲が文書化されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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<Base> factory()
{
return std::make_shared<Derived>(9);
}
std::shared_ptr<Base> factoryTwo()
{
return std::make_shared<DerivedTwo>(99);
}
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
class_<Base, std::shared_ptr<Base>, boost::noncopyable>("Base", init<>());
class_<Derived, bases<Base>, std::shared_ptr<Derived>, boost::noncopyable>("Derived", init<int>())
.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
<class 'mymodule.Derived'>
<class 'mymodule.Base'> <-- WHY?
Obviously, if I expose DerivedTwo class, all is OK.
Thanks for your help.
- 主要言語
- C++
- スター
- 537
- フォーク
- 223
- 平均マージ
- 11時間 22分
- マージ済み PR(30日)
- 2
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
boostorg/python のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 84/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmake オープン
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
boostorg/python の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
AXERA-TECH/ax-llm#77 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
games-on-whales/wolf#509 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
bug-unconfirmed
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100