开发者

Extract base class pointer

I was told that I can extract reference to base class from any boost::python::object.

The simple code looks like:

// c++:
class Base {};

// Export Base into python module

// python:
class Der(Base):
   pass

//c++:
boost::python::object obj; // It points to s开发者_JAVA百科ome Der class object made from python
boost::shared_ptr<Object> temp = extract< boost::shared_ptr<Object> >(obj);

The last line fails with:

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

Is it possible to extract pointer for Base class?


So, using this thread I've managed out my question. First of all, my Base class was exported into python this way:

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

I had to remove bp::no_init. Why? Look at next update (the answer in stackoverflow post I gave before):

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

That's it (:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜