こんにちわ
皆さんお元気ですか?私は金欠です。
さて、今回もBoost::python。
どうやらもう4回目、今回はクラスの継承、多重継承についてやりましょう。
さて、クラスの継承はこう書きます。
クラスの継承
C++
#include <boost/python.hpp> class Foo{ public: int temp; void value(int a){ temp = a; } int get_temp(){ return temp; } }; class Bar:public Foo{ public: void value(int a){ temp = a * 2; } }; BOOST_PYTHON_MODULE(inheritance){ using namespace boost::python; class_<Foo>("Foo") .def("value",&Foo::value) .def("get_temp",&Foo::get_temp); class_<Bar,bases<Foo> >("Bar") .def("value",&Bar::value) .def("get_temp",&Bar::get_temp); }
特筆する点は継承されたクラスの指定です。
class_<継承されたクラス,bases<継承するクラス> >と指定します。
後は特に変わってないですね。
python
>>> import inheritance >>> foo = inheritance.Foo() >>> foo.value(2) >>> foo.get_temp() 2 >>> bar = inheritance.Bar() >>> bar.value(2) >>> bar.get_temp()
そして、多重継承
多重継承
C++
#include <boost/python.hpp> #include <string> using namespace std; class Foo{ public: int temp; void value(int a){ temp = a; } int get_temp(){ return temp; } }; class Bar{ public: string temp2; void string_value(string a){ temp2 = a; } }; class Baz:public Foo,public Bar{ public: string get_temp(){ return temp2; } }; BOOST_PYTHON_MODULE(inheritance){ using namespace boost::python; class_<Foo>("Foo") .def("value",&Foo::value) .def("get_temp",&Foo::get_temp); class_<Bar>("Bar") .def("string_value",&Bar::string_value); class_<Baz,bases<Foo,Bar> >("Baz") .def("get_temp",&Baz::get_temp); }
bases<>内をFoo,Barにしているだけです。