运算符重载
来源:互联网 发布:淘宝上的韦恩国际代购 编辑:程序博客网 时间:2024/06/14 20:52
运算符重载的2种实现方式:
(1) 成员函数(参数少一个)
(2) 非成员函数(如果涉及类中非 public 成员,则需要在类中声明 friend,即友元函数)
1. 成员函数
#include <iostream>#include <algorithm> // find#include <vector>#include <string>using namespace std;struct CPerson{public: bool operator==(const CPerson &oPr) // 【1】只需要一个参数 { return (m_iAge == oPr.m_iAge); }public: int m_iAge; string m_strName;};int main(){ CPerson oP = {15, "Francis"}; CPerson arroPerson[] = {{19, "Alice"}, {15, "Bob"}, {29, "Mike"}}; CPerson *pP; // pointer to array element: pP = std::find (arroPerson, arroPerson+3, oP); std::cout << "15: " << pP->m_strName << '\n'; std::vector<CPerson> vecP(arroPerson, arroPerson+3); std::vector<CPerson>::iterator it; // iterator to vector element: it = find (vecP.begin(), vecP.end(), oP); std::cout << "15: " << it->m_strName << '\n'; return 0;}
2. 非成员函数
#include <iostream>#include <algorithm> // find#include <vector>#include <string>using namespace std;struct CPerson{ int m_iAge; string m_strName;};bool operator==(const CPerson &oPl, const CPerson &oPr){ return (oPl.m_iAge == oPr.m_iAge);}int main(){ CPerson oP = {15, "Francis"}; CPerson arroPerson[] = {{19, "Alice"}, {15, "Bob"}, {29, "Mike"}}; CPerson *pP; // pointer to array element: pP = std::find (arroPerson, arroPerson+3, oP); std::cout << "15: " << pP->m_strName << '\n'; std::vector<CPerson> vecP(arroPerson, arroPerson+3); std::vector<CPerson>::iterator it; // iterator to vector element: it = find (vecP.begin(), vecP.end(), oP); std::cout << "15: " << it->m_strName << '\n'; return 0;}
说明:运算符重载函数不一定要声明为友元函数,声明为友元函数的目的是获取访问权限,如果涉及的成员全是 public 的,则不需要声明为 friend
3. 原则——何时用成员函数,何时用非成员函数
STL 中 string 对运算符的重载
(1) 成员函数
=, +=
[]
(2) 非成员函数
+
==, !=, >, <, >=, <=
<<, >>
摘自:详细规则见:http://en.cppreference.com/w/cpp/language/operators
在此规则基础上,还有一些原则:
(1) 运算符具有可交换性时,重载为非成员函数更合适,如 "+", "=="等;
其它原则待补充
这里有一些其它原则:http://blog.chinaunix.net/uid-21411227-id-1826759.html
0 0
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- 运算符重载
- 有关运算符重载
- 游戏服务器之消息校验
- Tkinter中button按钮未按却主动执行command函数问题
- Android下DLNA和UPnP的研究以及问题
- CCS5.5 CMD文件小结
- ACM hdu 1019 Least Common Multiple
- 运算符重载
- ndk-stack定位crash
- JBPM4.4总结(一)——基础知识
- POJ2184
- 用友ERP U8软件安装
- SimpleAdapter与自定义Adapter的使用。
- connect by 自己学习看看
- Redis Sentinel的信息同步
- Android开发—Intent