Embed Python into the operator overloading of C ++. If you overload related operators in C ++. when Python is passed to Python, You can overload the "+" operator of the Msg class by using the following code. def (self + self) "is passed to Python.
- class Msg:public Message
- {
- public:
- int count;
- Msg(std::string m):Message(m)
- {
- }
- void setcount(int n)
- {
- count = n;
- }
- int getcount()
- {
- return count;
- }
- int operator+ (Msg x) const
- {
- int r;
- r = count + x.count;
- return r;
- }
- };
- BOOST_PYTHON_MODULE(Message)
- {
- class_<Message>("Message",init<std::string>())
- .add_property("msg",&Message::get,&Message::set);
- class_<Msg, bases<Message> >("Msg",init<std::string>())
- .def("setcount", &Msg::setcount)
- .def("getcount", &Msg::getcount)
- .def(self + self);
- }
You can use the same method to embed Python into the operator overload of C ++, as shown below.
. Def (self-self) // equivalent to the _ sub _ Method
. Def (self * self) // equivalent to the _ mul _ Method
. Def (self/self) // equivalent to the _ div _ Method
. Def (self <self); // equivalent to the _ lt _ Method
The above is an introduction to the content related to the heavy loading of operators embedded in Python C ++. I hope you will gain some benefits.