在python中使用c中的基类创建派生类++

2024-09-19 23:41:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类“Base”,它是派生的“MdBase”。
我还有一个班级工厂基地。
我必须定义MDBaseWrapper和FactoryBaseWrapper,因为这两个类中都有虚函数。你知道吗

BOOST_PYTHON_MODULE ( modulepython )
{
  class_<MySpace::FactoryBase, boost::noncopyable>( "FactoryBaseCpp", no_init );
  class_<MySpace::Base, boost::noncopyable>( "Base", no_init );
  class_<MySpace::MdBase, boost::noncopyable>( "MdBaseCpp", no_init );
  class_<MySpace::Key, boost::noncopyable>( "Key", no_init )
  .add_property( "sourceTradable",
                 make_getter( &MySpace::Key::sourceTradable, return_internal_reference<>()));
  class_<Interface::Tradable, boost::noncopyable>( "Tradable", no_init );

  class_<FactoryBaseWrapper, boost::noncopyable>( "FactoryBase", no_init )
  .def( init<const std::string&, const MySpace::FactoryConfig&>())
  .def( init<const std::string&, MySpace::FactoryMgr&, const std::string&, bool>())
  .def( "getName", &FactoryBaseWrapper::getName, return_internal_reference<>())
  .def( "create", &FactoryBaseWrapper::create, return_internal_reference<>());

  class_<MDBaseWrapper, boost::noncopyable>( "MdBase", init<MDBaseWrapper>())
  .def( init<MySpace::FactoryBase&, const MySpace::Key&,
  const Interface::Tradable&, int, int>())  
}

还有一个简单的python脚本。 工厂只返回一个简单的MdBase派生类。你知道吗

 import modulePython as pyMod 

class DerivedTest(pyMod.MdBase):
    def __init__(self, factory, key, tradable, exchangeType, changeMask):
        super(DerivedTest, self).__init__(factory, key, tradable, exchangeType, changeMask)
        print "Test"


class PythonFactory(pyMod.FactoryBase):
    def __init__(self):
        print "Creating Factory"

    def create(self, key):
        print "Creating Test"
        return DerivedTest(self, key, key.sourceTradable, 0, 0)

当我从cpp调用“create”时,我得到了他的错误。你知道吗

Traceback (most recent call last):
  File "PyFactory.py", line 21, in create
    return DerivedTest(self, key, key.sourceTradable, 0, 0)
  File "PyFactory.py", line 8, in __init__
    super(DerivedTest, self).__init__(factory, key, tradable, Type, changeMask)
Boost.Python.ArgumentError: Python argument types in
    MdBase.__init__(DerivedTest, PythonFactory, Key, Tradable, int, int)
did not match C++ signature:
    __init__(_object*, MySpace::FactoryBase {lvalue}, MySpace::Key, Interface::Tradable, int, int)
    __init__(_object*, MDBaseWrapper)
line 51:  4306 Segmentation fault  (core dumped)

因此,我认为python可能没有获得类之间的继承,并将其更改为这样以指示包装器继承自FactoryBase。你知道吗

 class_<FactoryBaseWrapper, bases<MySpace::FactoryBase>, boost::noncopyable>( "FactoryBase", no_init )

现在,我在那一行发现了一个编译错误。你知道吗

boost_1_55_0/boost/python/object/class_metadata.hpp:57: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::not_<boost::is_same<MySpace::FactoryBase, MySpace::FactoryBase> >::************)’

你看到代码里有什么不对吗? 谢谢


Tags: keynoselfinitdefclassintboost