Python 调用 C++
- 作者: 过来给你看个宝贝
- 来源: 51数据库
- 2022-08-12
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/to_python_converter.hpp>
#include
using namespace std;
using namespace boost::python;
namespace HelloPython{
// 简单函数
char const* sayHello(){
return "Hello from boost::python";
}
// 简单类
class HelloClass{
public:
HelloClass(const string& name):name(name){
}
public:
string sayHello(){
return "Hello from HelloClass by : " + name;
}
private:
string name;
};
// 接受该类的简单函数
string sayHelloClass(HelloClass& hello){
return hello.sayHello() + " in function sayHelloClass";
}
//STL容器
typedef vector<int> ivector;
//有默认参数值的函数
void showPerson(string name,int age=30,string nationality="China"){
cout << name << " " << age << " " << nationality << endl;
}
// 封装带有默认参数值的函数
BOOST_PYTHON_FUNCTION_OVERLOADS(showPerson_overloads,showPerson,1,3) //1:最少参数个数,3最大参数个数
// 封装模块
BOOST_PYTHON_MODULE(HelloPython){
// 封装简单函数
def("sayHello",sayHello);
// 封装简单类,并定义__init__函数
class_("HelloClass",init())
.def("sayHello",&HelloClass::sayHello)//Add a regular member function
;
def("sayHelloClass",sayHelloClass); // sayHelloClass can be made a member of module!!!
// STL的简单封装方法
class_("ivector")
.def(vector_indexing_suite());
class_ >("ivector_vector")
.def(vector_indexing_suite >());
// 带有默认参数值的封装方法
def("showPerson",showPerson,showPerson_overloads());
}
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
