Introduction to embedding Python into the member attributes of C ++ classes

Source: Internet
Author: User

In C ++, key words can be used to declare related members of classes as different attributes. However, in Python, it depends on the naming method of class-related attributes. The following article describes how to embed Python into the member attributes of classes in C ++.

Embedding Python into the member attributes of C ++ classes

In C ++, key words can be used to declare related members of classes as different attributes. However, in Python, it depends on the naming method of class-related attributes. You can use Boost. Python to pass the attributes of class members in C ++ to Python. The following code uses Boost. Python to process attributes of class members. Change the code in BOOST_PYTHON_MODULE to the following.

 
 
  1. BOOST_PYTHON_MODULE(Message)  
  2. {  
  3. class_<Message>("Message", init<std::string>())  
  4. .def("set", &Message::set)  
  5. .def("get", &Message::get)  
  6. .def_readwrite("msg", &Message::msg);  
  7. ;  
  8. }   

Set msg, a member of the Message class, to read/write. You can also set it to read-only by using ". def_readonly. For private members in the class, you can also use ". add_property" to set its operation function to an attribute in the Python class. The following code uses ". add_property" to operate private members.

 
 
  1. BOOST_PYTHON_MODULE(Message)  
  2. {  
  3. class_<Message>("Message",init<std::string>())  
  4. .add_property("msg",&Message::get,&Message::set);  
  5. }  

The following code uses the compiled Message module in Python.

 
 
  1. >>> import Message  
  2. >>> s = Message.Message('hi')  
  3. >>> s.msg  
  4. 'hi'  
  5. >>> s.msg = 'boot' 
  6. >>> s.msg  
  7. 'boot'  

4. class inheritance

The inheritance relationships of classes in C ++ can also be reflected in the Python module through Boost. Python. The following code imports the parent class and subclass into the Python module respectively. The following describes the member attributes of classes embedded in C ++ in Python.

 
 
  1. #include <string.h> 
  2. #include <iostream.h> 
  3. #include <boost/python.hpp> 
  4. using namespace boost::python;  
  5. #pragma comment(lib, "boost_python.lib")  
  6. class Message  
  7. {  
  8. public:  
  9. std::string msg;  
  10. Message(std::string m)  
  11. {  
  12. mmsg = m;  
  13. }  
  14. void set(std::string m)  
  15. {  
  16. mmsg = m;  
  17. }  
  18. std::string get()  
  19. {  
  20. return msg;  
  21. }  
  22. };  
  23. class Msg:public Message  
  24. {  
  25. public:  
  26. int count;  
  27. Msg(std::string m):Message(m)  
  28. {  
  29. }  
  30. void setcount(int n)  
  31. {  
  32. count = n;  
  33. }  
  34. int getcount()  
  35. {  
  36. return count;  
  37. }  
  38. };  
  39. BOOST_PYTHON_MODULE(Message)  
  40. {  
  41. class_<Message>("Message",init<std::string>())  
  42. .add_property("msg",&Message::get,&Message::set);  
  43. class_<Msg, bases<Message> >("Msg",init<std::string>())  
  44. .def("setcount", &Msg::setcount)  
  45. .def("getcount", &Msg::getcount);  

The above is an introduction to the member attributes of the class embedded in C ++ in Python. I hope you will gain some benefits.

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.