Detailed operation steps for embedding Python into C ++ Operator Overloading

Source: Internet
Author: User

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.

 
 
  1. class Msg:public Message  
  2. {  
  3. public:  
  4. int count;  
  5. Msg(std::string m):Message(m)  
  6. {  
  7. }  
  8. void setcount(int n)  
  9. {  
  10. count = n;  
  11. }  
  12. int getcount()  
  13. {  
  14. return count;  
  15. }  
  16. int operator+ (Msg x) const  
  17. {  
  18. int r;  
  19. r = count + x.count;  
  20. return r;  
  21. }  
  22. };  
  23. BOOST_PYTHON_MODULE(Message)  
  24. {  
  25. class_<Message>("Message",init<std::string>())  
  26. .add_property("msg",&Message::get,&Message::set);  
  27. class_<Msg, bases<Message> >("Msg",init<std::string>())  
  28. .def("setcount", &Msg::setcount)  
  29. .def("getcount", &Msg::getcount)  
  30. .def(self + self);  
  31. }  

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.