Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
覆盖一个C++;Python中使用Boost.Python的虚拟函数? 我有一个C++类,它有一个虚拟方法:_Python_C++_Boost_Virtual_Boost Python - Fatal编程技术网

覆盖一个C++;Python中使用Boost.Python的虚拟函数? 我有一个C++类,它有一个虚拟方法:

覆盖一个C++;Python中使用Boost.Python的虚拟函数? 我有一个C++类,它有一个虚拟方法:,python,c++,boost,virtual,boost-python,Python,C++,Boost,Virtual,Boost Python,//C++ 甲级 { }) 我想做的是使用Boost.Python将这个类公开给Python,在Python中继承这个类,并将正确的重写名为: #python: class B(A): def override_me(self, a): return 5*a b = B() b.calculate(1) # should return 5 instead of 2 对于纯虚拟函数,我也希望这样做。我正在寻找一种不在C++中创建任何包装类的方法,这是可能的吗?如果是/如果不是

//C++ 甲级 {

})

我想做的是使用Boost.Python将这个类公开给Python,在Python中继承这个类,并将正确的重写名为:

#python:
class B(A):
   def override_me(self, a):
       return 5*a
b = B()
b.calculate(1) # should return 5 instead of 2

对于纯虚拟函数,我也希望这样做。我正在寻找一种不在C++中创建任何包装类的方法,这是可能的吗?如果是/如果不是,我该怎么做?

您可以在类周围提供一个薄包装器,将
重写方法调用委托给
boost::python
特定的重写函数

派生类<代码>计算< /COD>调用只调用父类<代码>计算< /C>方法,因此当它们从Python调用时,调用C++定义的<代码>计算< /C>方法,但仍然允许从Python中重写<代码> OrrReDyMe<代码>方法:

#include <boost/python.hpp>
using namespace boost;
using namespace boost::python;

class A {

public:
    A() {};
    virtual int override_me(int a) {
        return 2*a;
    };
    virtual int calculate(int a) {
        return this->override_me(a);
    }
};

struct AWrap: A, public boost::python::wrapper<A> {
    AWrap() : A() {};
    int override_me(int a) override {
        if (override f = this->get_override("override_me")) {
            return this->get_override("override_me")(a);
        } else {
            return A::override_me(a);
        }
    };
    int calculate(int a) override {
        return A::calculate(a);
    }
};

BOOST_PYTHON_MODULE(my_lib)
{
      python::class_<AWrap, boost::noncopyable>("A", python::init<>())
      .def("override_me", &AWrap::override_me)
      .def("calculate", &AWrap::calculate);
}

int main() {}
import my_lib

class B(my_lib.A):
   def override_me(self, a):
       return 5*a
b = B()

print (b.calculate(1))
二,

但是,可以从Python进行虚拟覆盖:

#include <boost/python.hpp>
using namespace boost;
using namespace boost::python;

class A {

public:
    A() {};
    virtual int override_me(int a) {
        return 2*a;
    };
    virtual int calculate(int a) {
        return this->override_me(a);
    }
};

struct AWrap: A, public boost::python::wrapper<A> {
    AWrap() : A() {};
    int override_me(int a) override {
        if (override f = this->get_override("override_me")) {
            return this->get_override("override_me")(a);
        } else {
            return A::override_me(a);
        }
    };
    int calculate(int a) override {
        return A::calculate(a);
    }
};

BOOST_PYTHON_MODULE(my_lib)
{
      python::class_<AWrap, boost::noncopyable>("A", python::init<>())
      .def("override_me", &AWrap::override_me)
      .def("calculate", &AWrap::calculate);
}

int main() {}
import my_lib

class B(my_lib.A):
   def override_me(self, a):
       return 5*a
b = B()

print (b.calculate(1))
五,


在C++中,不要在类中创建任何包装类——“需求有多强?”没有严格的要求。如果它是可以实现的,那么它的优点和优点是将C++类暴露给Python的标准方法:Posith:Python,你尝试过了吗?(我个人推荐pybind11而不是boost::python,这两个包在精神上非常相似,所以思想和技术可以延续下去)。看起来像是你想要的,但我无法让它工作:-为什么你会推荐pybind11而不是boost@n.代词m。