Base class of non exposed derived class in Python side?

Đang mở
#415 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp
Lĩnh vực
api

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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của boostorg/python

Tất cả issue của boostorg/python

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.