In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the Python embedded in C++ class member attributes of the example analysis, the editor thinks it is very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
In C++, relevant members of a class can use keywords to declare different properties. In Python, however, it depends on how the relevant properties of the class are named.
Embed Python into the member attribute of a class in C++
In C++, relevant members of a class can use keywords to declare different properties. In Python, however, it depends on how the relevant properties of the class are named. Using Boost.Python, you can pass properties of class members in its C++ to Python. The code shown below uses Boost.Python to handle the properties of class members. Change the code in BOOST_PYTHON_MODULE to look like this.
BOOST_PYTHON_MODULE (Message) {class_ ("Message", init ()) .def ("set", & Message::set) .def ("get", & Message::get) .def _ readwrite ("msg", & Message::msg);;}
Here, make the member msg in the Message class read-write, and you can also set it as a read-only property using ".def _ readonly". For private members of a class, you can also use ".add _ property" to set its action function to a property in the Python class. The following code uses ".add _ property" to operate on private members.
BOOST_PYTHON_MODULE (Message) {class_ ("Message", init ()) .add _ property ("msg", & Message::get,&Message::set);}
The following code uses the compiled Message module in Python.
> import Message > s = Message.Message ('hi') > s.msg' hi' > s.msg = 'boot' > s.msg' boot'
4. Inheritance of classes
The inheritance of classes in C++ can also be reflected in the Python module through Boost.Python. The following code imports the parent and subclasses into the Python module, respectively. The following are the member attributes of the class embedded in C++ for Python
# include # include # include using namespace boost::python; # pragma comment (lib, "boost_python.lib") class Message {public: std::string msg; Message (std::string m) {mmsg = m;} void set (std::string m) {mmsg = m;} std::string get () {return msg;}}; class Msg:public Message {public: int count Msg (std::string m): Message (m) {} void setcount (int n) {count = n;} int getcount () {return count;}}; 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); the above is the example analysis of embedding Python into the member attributes of classes in C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.