2011-04-10 45 views
0

有人告訴我可以從任何boost::python::object中提取對基類的引用。提取基類指針

簡單的代碼如下所示:

// c++: 
class Base {}; 

// Export Base into python module 

// python: 
class Der(Base): 
    pass 

//c++: 
boost::python::object obj; // It points to some Der class object made from python 
boost::shared_ptr<Object> temp = extract< boost::shared_ptr<Object> >(obj); 

最後一行失敗:

TypeError: No registered converter was able to produce a C++ rvalue of type boost::shared_ptr from this Python object of type Der

是否可以提取指針Base類?

回答

2

所以,使用this thread我已經處理了我的問題。首先,我Base類是出口到蟒蛇是這樣的:

bp::class_<Base, boost::noncopyable>("Base", bp::no_init) 

,我不得不刪除bp::no_init。爲什麼?看看下一次更新(在計算器後的答案,我之前給):

class Der(Base): 
    def __init__(self): 
     super(Der, self).__init__() # Add this! 

就是這樣(: