1. 可以重载的运算符
- 算法运算符 + - * / %
- 逻辑运算符 && || !
- 关系运算符 > < = >= <= == !=
- 位运算符 ~ << >> & ^ |
- 自增自减运算符 ++ –
- 复合赋值运算符 += -= *= /= %=
2.不能重载的运算符
- ?: 或 * 或 ::(域) 或 sizeof
- 不需要重载的运算符 = (赋值)和 & (取地址符)
- 赋值函数
3.dmeo
头文件
#ifndef TIME_H
#define TIME_H
class CTime {
public:
CTime(int hour, int minute, int second);
// 赋值运算符
CTime& operator=(CTime& time);
private:
int m_nHour;
int m_nMinute;
int m_nSecond
};
#endif
实现
#include "Time.h"
CTime::CTime(int hour, int minute, int second)
{
m_nHour = hour;
m_nMinute = minute;
m_nSecond = second;
}
// 赋值运算符
CTime& CTime :: operator=(CTime& time){
if(this == &time) {
return *this;
}
m_nHour = time.m_nHour;
m_nMinute = time.m_nMinute;
m_nSecond = time.m_nSecond;
return *this;
}