本章描述c++类,如何被python识别和使用
c++代码:src.cpp
#include <iostream>
#include <string>
using namespace std;
/**********************************************struct and class ***********************************/
struct A
{
void greet()
{
cout << "just a test! from A" << endl;
}
};
struct B
{
B(string msg): msg(msg) {}
void set(string msg)
{
this->msg = msg;
}
void greet()
{
cout << msg << endl;
}
string msg;
};
class C
{
public:
void greet()
{
cout << "just a test! from C" << endl;
}
};
#include <string>
using namespace std;
/**********************************************struct and class ***********************************/
struct A
{
void greet()
{
cout << "just a test! from A" << endl;
}
};
struct B
{
B(string msg): msg(msg) {}
void set(string msg)
{
this->msg = msg;
}
void greet()
{
cout << msg << endl;
}
string msg;
};
class C
{
public:
void greet()
{
cout << "just a test! from C" << endl;
}
};
pyhton转换代码:src4py.cpp
#include <boost/python.hpp>
#include "src.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
class_<A>("A")
.def("greet", &A::greet)
;
class_<B>("B",init<string>()) //注意构造函数的写法
.def("greet", &B::greet)
.def("set", &B::set)
;
class_<C>("C")
.def("greet", &C::greet)
;
//Constructors overload
class_<D>("D",init<string,int>())
.def(init<string>())
.def("printstr",&D::printstr)
.def("printint",&D::printint)
;
//if we do not wish to expose any constructors in E at all
class_<E>("E",no_init)
.def("printstr",&E::printstr)
;
class_<F>("F")
.def("initE",&F::initE)
.def("printEstr",&F::printEstr)
;
}
#include "src.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
class_<A>("A")
.def("greet", &A::greet)
;
class_<B>("B",init<string>()) //注意构造函数的写法
.def("greet", &B::greet)
.def("set", &B::set)
;
class_<C>("C")
.def("greet", &C::greet)
;
//Constructors overload
class_<D>("D",init<string,int>())
.def(init<string>())
.def("printstr",&D::printstr)
.def("printint",&D::printint)
;
//if we do not wish to expose any constructors in E at all
class_<E>("E",no_init)
.def("printstr",&E::printstr)
;
class_<F>("F")
.def("initE",&F::initE)
.def("printEstr",&F::printEstr)
;
}
编译成so过程省略,如果要看makefile文件,请参照第一篇文章
python中调用代码test.py
import test
obj = test.A() //对应c++的struct A
obj.greet() //structA中的方法greet
obj = test.B("just a test! from B") //对应c++的struct B,且含构造函数
obj.greet()
obj.set("test again! from B")
obj.greet()
obj = test.C()
obj.greet()
obj = test.D("just a test!",1)
obj.printstr()
obj.printint()
#if we do not wish to expose any constructors in E at all
obj = test.F()
e = obj.initE("just a test!")
e.printstr()
obj.printEstr()
obj = test.A() //对应c++的struct A
obj.greet() //structA中的方法greet
obj = test.B("just a test! from B") //对应c++的struct B,且含构造函数
obj.greet()
obj.set("test again! from B")
obj.greet()
obj = test.C()
obj.greet()
obj = test.D("just a test!",1)
obj.printstr()
obj.printint()
#if we do not wish to expose any constructors in E at all
obj = test.F()
e = obj.initE("just a test!")
e.printstr()
obj.printEstr()
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751822,如需转载请自行联系原作者