Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the procedure for embedding Python into the operator overload of C++

Shulou Source: shulou.com Published: 2022-06-02 23:25:52 10月18日 Update

This article introduces the operation procedure of operator overloading of embedding Python into C++. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Embed Python into the operator overload of C++ if you overload the related operator in C++ and pass Boost.Python to Python, you can overload the "+" operator of the Msg class and pass it to Python through ".def (self + self)" in the following code.

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", init ()) .add _ property ("msg", & Message::get,&Message::set); class_ ("Msg", init ()) .def ("setcount", & Msg::setcount) .def ("getcount", & Msg::getcount) .def (self + self);}

Embedding Python in C++ 's operator overloading works the same way for other operator overloads, 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

This is the end of the procedure for embedding Python into C++ 's operator overloading. I hope the above can be of some help and learn more. If you think the article is good, you can share it for more people to see.

Tags: Operators operations clocking methods steps content more help good code interests guys buddies articles knowledge articles references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS Redmi Docker vpn