把Python嵌入C++的运算符重载的操作步骤详解

开发 后端
把Python嵌入C++的运算符重载中的实际操作步骤,你对其是否感兴趣?如果想在关于Python嵌入C++的运算符重载有更好的了解,那你可以浏览下面的文章。

把Python嵌入C++的运算符重载中你如果在C++中对相关运算符重载后,把Boost.Python传给Python时,你就可以将以下的代码将Msg类的“+”运算符重载,然后通过“.def(self + self)”传递给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. }  

 

把Python嵌入C++的运算符重载中对于其他的运算符重载也可以使用同样的方法,如下所示。

.def(self - self) // 相当于_sub_方法

.def(self * self) // 相当于_mul_方法

.def(self /self) // 相当于_div_方法

.def(self < self); // 相当于_lt_方法

 以上就是对Python嵌入C++的运算符重载相关的内容的介绍,望你会有所收获。

【编辑推荐】

  1. Python嵌入C/C++(较低层次嵌入)时所需用到的函数
  2. Python嵌入C/C++会产生的强大功能的介绍
  3. Python嵌入C++来弥补C++本身不足之处的方案介绍
  4. 用Python连接PostgreSQL数据库所需版本的介绍
  5. Python连接数据库的实际操作方案的介绍
责任编辑:佚名 来源: 互联网
相关推荐

2009-08-12 12:46:11

C#运算符重载

2011-07-15 01:34:36

C++重载运算符

2024-01-26 16:37:47

C++运算符开发

2011-07-15 10:08:11

C++运算符重载

2020-08-10 10:20:15

流插入运算符语言

2009-08-12 10:37:13

C#运算符重载

2009-08-12 10:47:03

C#运算符重载

2010-03-26 11:00:55

Python嵌入CC++

2009-11-06 13:57:52

C#

2020-09-30 14:04:25

C++运算符重载

2009-08-12 10:27:12

C#运算符重载运算符重载实例

2009-09-04 13:18:10

C#允许运算符重载

2009-08-12 10:56:47

C#运算符重载C#运算符重载实例

2009-08-14 10:16:57

C#运算符重载

2009-08-12 11:20:51

C#运算符重载

2021-12-15 10:25:57

C++运算符重载

2010-03-26 11:00:55

Python嵌入CC++

2010-02-03 15:40:11

C++地址运算符

2023-09-07 23:30:47

运算符C++

2010-03-24 13:17:35

Python嵌入
点赞
收藏

51CTO技术栈公众号