日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

Boost.Python实现Python C/C++混合编程

發布時間:2023/12/10 c/c++ 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Boost.Python实现Python C/C++混合编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導出函數

#include<string> #include<boost/python.hpp>using namespace std; using namespace boost::python;char const * greet() {return "hello,world";}BOOST_PYTHON_MODULE(hello_ext) {def("greet", greet); } import hello_ext print hello_ext.greet()

?

導出默認構造的函數的類

#include<string> #include<boost/python.hpp>using namespace std; using namespace boost::python;struct World {void set(string msg) { this->msg = msg; }string greet() { return msg; }string msg; };BOOST_PYTHON_MODULE(hello) //導出的module 名字 {class_<World>("World").def("greet", &World::greet).def("set", &World::set); } import hello planet = hello.World() # 調用默認構造函數,產生類對象 planet.set("howdy") # 調用對象的方法 print planet.greet() # 調用對象的方法

?

構造函數的導出:

#include<string> #include<boost/python.hpp>using namespace std; using namespace boost::python;struct World {World(string msg):msg(msg){} //增加構造函數World(double a, double b):a(a),b(b) {} //另外一個構造函數void set(string msg) { this->msg = msg; }string greet() { return msg; }double sum_s() { return a + b; }string msg;double a;double b; };BOOST_PYTHON_MODULE(hello) //導出的module 名字 {class_<World>("World",init<string>()) .def(init<double,double>()) // expose another construct.def("greet", &World::greet).def("set", &World::set).def("sum_s", &World::sum_s); } import hello planet = hello.World(5,6) planet2 = hello.World("hollo world")print planet.sum_s() print planet2.greet()

?

如果不想導出任何構造函數,則使用no_init:

class_<Abstract>("Abstract",no_init)

?

類的數據成員

#include<string> #include<boost/python.hpp>using namespace std; using namespace boost::python;struct Var {Var(string name):name(name),value(){}string const name;float value; };BOOST_PYTHON_MODULE(hello_var) {class_<Var>("Var", init<string>()).def_readonly("name", &Var::name) //只讀.def_readwrite("value", &Var::value); //讀寫 } import hello_varvar = hello_var.Var("hello_var") var.value = 3.14 # var.name = 'hello' # error print var.name

?

類的屬性

// 類的屬性#include<string> #include<boost/python.hpp>using namespace std; using namespace boost::python;struct Num {Num(){}float get() const { return val; }void set(float val) { this->val = val; }float val;};BOOST_PYTHON_MODULE(hello_num) {class_<Num>("Num").add_property("rovalue", &Num::get) // 對外:只讀.add_property("value", &Num::get, &Num::set);// 對外讀寫 .value值會改變.rovalue值,存儲著同樣的數據。} import hello_num num = hello_num.Num() num.value = 10 print num.rovalue # result: 10

?

繼承

// 類的繼承#include<string> #include<iostream> #include<boost/python.hpp>using namespace std; using namespace boost::python;struct Base {virtual ~Base() {};virtual string getName() { return "Base"; }string str; };struct Derived : Base {string getName() { return "Derived"; }};void b(Base *base) { cout << base->getName() << endl; };void d(Derived *derived) { cout << derived->getName() << endl; };Base * factory() { return new Derived; }/*下面的額外的代碼如果去掉會報錯。解決地址:http://stackoverflow.com/questions/38261530/unresolved-external-symbols-since-visual-studio-2015-update-3-boost-python-link/38291152#38291152 */ namespace boost {template <>Base const volatile * get_pointer<class Base const volatile >(class Base const volatile *c){return c;} }BOOST_PYTHON_MODULE(hello_derived) {class_<Base>("Base").def("getName", &Base::getName).def_readwrite("str", &Base::str);class_<Derived, bases<Base> >("Derived").def("getName", &Derived::getName).def_readwrite("str", &Derived::str);def("b", b);def("d", d);def("factory", factory,return_value_policy<manage_new_object>());//} import hello_derived derive = hello_derived.factory() hello_derived.d(derive)

?

類的虛函數:

/*類的虛函數,實現的功能是:可以編寫Python類,來繼承C++類 */ #include<boost/python.hpp>#include<boost/python/wrapper.hpp> #include<string> #include<iostream>using namespace boost::python; using namespace std;struct Base {virtual ~Base() {}virtual int f() { return 0; }; };struct BaseWrap : Base, wrapper<Base> {int f(){if (override f = this->get_override("f"))return f(); //如果函數進行重載了,則返回重載的return Base::f(); //否則返回基類}int default_f() { return this->Base::f(); } };BOOST_PYTHON_MODULE(hello_virtual) {class_<BaseWrap, boost::noncopyable>("Base").def("f", &Base::f, &BaseWrap::default_f);} import hello_virtualbase = hello_virtual.Base() # 定義派生類,繼承C++類 class Derived(hello_virtual.Base):def f(self):return 42derived = Derived()print base.f()print derived.f()

?

總結

以上是生活随笔為你收集整理的Boost.Python实现Python C/C++混合编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。