This chapter first explains how to encapsulate the constructor, method, and data member of a class by encapsulating a simple class.
#include <boost/python.hpp> #include <iostream>using namespace boost::p Ython; Class Animal{public: animal () { mylanguage= ""; }; Animal (std::string language) { mylanguage=language; }; Std::string get () { return mylanguage; } Std::string mylanguage;}; Boost_python_module (Cry) { class_<animal> ("Animal") . Def (init<std::string> ())//Wrap for Multipole Constructors . def ("get", &animal::get) . Def_readonly ("language", &animal::mylanguage) ; or set it as Def_readwrite, note:for Private member, we cannot wrap it here}
The package class starts with the BOOST macro, Boost_python_module (MODULE name). Define the part that needs to be encapsulated within the module: class_< is encapsulated class > (the name of the class corresponding to module) plus. Def (method name, Method object). For the data member is special, first of all must not make private, this no matter how you play will not compile through (Python does not support the friend mechanism?). Then use. def_readonly is not able to modify the only readable, equivalent to the const XXX member, or def_readwrite, then is read and write.
To compile a run test:
Python 2.7.5 (Default, Nov 3, 14:26:24) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on Linux2type "help", "copyright", "Credits" or "license" for more information.>>> import cry>>> a=cry.animal () >>> B=cry.animal ("Hello") >>> print a.get () >>> print b.get () hello>>> a.language ">>> b.language" Hello ' >>> b.language= ' test ' Traceback (most recent call last):
This kind of wrapper is valid for the General class, unfortunately under the GUI ... Especially the evil Qt, this lib is useless ....So how does python encapsulate the inheritance and polymorphism of C + + ?。。。
Make a slight change to the example above
#include <boost/python.hpp> #include <iostream>using namespace boost::p Ython; Class Animal{public: animal () { mylanguage= ""; }; Animal (std::string language) { mylanguage=language; }; Std::string get () { return mylanguage; } Protected: std::string mylanguage;}; Class Dog:p ublic animal{public: dog () { mylanguage= "bak bak"; }; Dog (std::string language) : Animal (language) { };}; void Mycry (animal* it) { std::cout<<it->get () <<std::endl;} Boost_python_module (Cry) { class_<animal> ("Animal") . Def (init<std::string> ()) . def (" Get ", &animal::get); Class_<dog, bases<animal> > ("Dog") . Def (init<std::string> ()); def ("Mycry", Mycry);}
Now there is a class called Dog that inherits Animal, whose constructor differs from animal, and has a mycry function to test polymorphism.
Class_<dog, bases<animal> > ("Dog") . Def (init<std::string> ());
As long as the use of bases<animal>, there is no need for def get, because in animal has been def once, wrapper automatically inherit over.
Python 2.7.5 (Default, Nov 3, 14:26:24) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on Linux2type "help", "copyright", "Credits" or "license" for more information.>>> import cry>>> a=cry.animal () >>> B=cry.dog () & Gt;>> Mycry (a) >>> cry.mycry (a) >>> cry.mycry (b) Bak bak
Virtual functions are another scenario that needs to be handled. This wrapper needs a boost wrap from c++wrap once.
Then make a simple change:
#include <boost/python.hpp> #include <iostream>using namespace boost::p Ython; Class Animal{public: virtual std::string get () { return "virtual animal, not implemented"; } Virtual ~animal () {};}; Class Dog:p ublic animal{public: virtual std::string get () { return "bak bak"; } Virtual ~dog () {};};/ /wrapper for Animalclass animalwrap:public animal,public wrapper<animal>{public: std::string get () { if (override Get=this->get_override ("get")) return get (); return Animal::get (); } std::string default_get () { return this->animal::get (); }}; void Mycry (animal* it) { std::cout<<it->get () <<std::endl;} Boost_python_module (Cry) { class_<animalwrap,boost::noncopyable> ("Animal") . def ("Get",& Animal::get,&animalwrap::d efault_get); Class_<dog, bases<animal> > ("Dog"); def ("Mycry", Mycry);}
The get is now a virtual function, and the return string is different. One of the wrapper written separately:
Wrapper for Animalclass animalwrap:public animal,public wrapper<animal>{public: std::string get () { C2/>if (Override Get=this->get_override ("get")) return get (); return Animal::get (); } std::string default_get () { return this->animal::get (); }};
An inheritance animal, a succession of wrapper<animal> and the second is boost for you to do the wrapper, which requires implement two functions, get and Default_get, Get inside checks if there is overload, if yes, returns overloaded, not return the parent class. As for why a default_get, I did not understand doc, but do it is work. Let me know if anyone knows.
In boost wrapper, two of them have wrap.
Class_<animalwrap,boost::noncopyable> ("Animal")
. def ("Get", &animal::get,&animalwrap::d efault_get);
Note that the Class_ is followed by wrap class name, and added an unknown why the boost::noncopyable, the next two of our wrapper defined function are put in.
After compiling, define a subclass of animal called Cat in Python, and use mycry to detect whether it works as expected
>>> from Cry import *>>> a=animal () >>> B=dog () >>> class Cat (animal): ... def get (self): ...
Python.boost Study notes (4)