Base class of non exposed derived class in Python side?
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Tái hiện ví dụ bằng cách sử dụng các định nghĩa Base, Derived, DerivedTwo, factory và factoryTwo được nêu. Trước tiên, hãy đọc các đăng ký class_ của Boost.Python, khai báo bases và hành vi chuyển đổi của shared_ptr; so sánh các kiểu Python được trả về khi có và không đăng ký DerivedTwo. Công việc được xem là hoàn tất khi hành vi lựa chọn kiểu được giải thích và cách xử lý dự kiến hoặc phạm vi của một fix được ghi lại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- C++
- Star
- 537
- Fork
- 223
- Merge trung bình
- 11 giờ 22 phút
- Pull request đã merge (30 ngày)
- 2
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của boostorg/python
-
numpy::from_data calls vector::front and crashes (or has UB) when called with a scalar object Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmake Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
Tất cả issue của boostorg/python
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
AXERA-TECH/ax-llm#77 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
games-on-whales/wolf#509 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
bug-unconfirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100