一、基本写法
- 功能:类内重新解释运算符,类对象会调用重载的运算符
- 代码示例(运算符重载、类对象数组、const修饰this、this)
#include <iostream> using namespace std; class Time { private: int _hours; public: Time(int h = 0){_hours = h;}; ~Time(){}; // const修饰this指针 void show() const{cout << "时间是:" << _hours << endl;}; // 固定写法:重载加法运算符写法 Time operator+(const Time &t) const; // 固定写法:重载乘法运算符写法 Time operator*(double n) const; }; Time Time::operator+(const Time &t) const { Time sum; sum._hours = this->_hours + t._hours; return sum; }; // const修饰this Time Time::operator*(double n) const { Time sum; sum._hours = this->_hours * n; return sum; }; int main() { Time t[3] = {Time(1), Time(2)}; // 相当于t[2] = t[0].operator+(t[1]);,可以连加 t[2] = t[0] + t[1]; t[2].show(); // 时间是:3 // 相当于t[2] = t[0].operator*(10); t[2] = t[0] * 10; t[2].show(); // 时间是:10 }
- 可重载运算符
符号名 符号名 符号名 +、-、*、/、%、= +=、-=、*=、/=、%/ >、<、>=、<=、==、!= >>、<< ++、- -、-(负号) []、() new、delete new[]、delete[] ||、&& , -> 其他略
二、单目运算符重载
- 代码示例(this、单目运算符-重载、const)
#include <iostream> using namespace std; class Time { private: int _hours; public: Time(int hours = 0) {_hours = hours;}; ~Time(){}; void show() const{cout << "时间是:" << _hours << endl;}; // 重载单目运算符:形参为空为标志性判别,this隐含传入 Time operator-() const; }; Time Time::operator-() const { // _hours是this的 return Time(-_hours); }; int main() { Time t = Time(1); // 调用单目运算符:-t t = -t; t.show(); // 时间是:-1 }
三、友元函数、类分区
- 功能:通过让函数成为类的友元,赋予该函数与类成员函数相同的访问权限
- 代码示例:(类分区、类枚举变量赋值和定义、友元函数重载运算符*和<<)
#include <iostream> using namespace std; class Time { //------------------a公有数据区------------------// public: // 状态成员:类枚举,作用域为类内,标识对象所处状态 enum Mode { ASIA, EUROPE }; //------------------b私有数据区------------------// private: int _hours; // mode只能存储:ASIA或EUROPE Mode mode; //------------------c公有方法区------------------// public: //-------------c1初始化及析构函数-------------// Time(int h=0, Mode form = ASIA); ~Time(){}; //-------------c2常规方法-------------------// //-------------c3运算符重载-----------------// // 类常规*运算符重载:只能运算Time*double Time operator*(double n) const; //-------------c4友元----------------------// // 友元函数运算符重载:只能运算double*Time,按顺序传入,可访问私有数据 friend Time operator*(double m, const Time &t); // 友元函数:只能运算cout<<Time,按顺序传入 friend ostream &operator<<(ostream &os, const Time &t); }; Time::Time(int h, Mode form) { _hours = h; mode = form; } // 类常规*运算符重载 Time Time::operator*(double n) const { Time sum; sum._hours = this->_hours * n; sum.mode = this->mode; cout << "调用了*重载函数" << endl; return sum; } // 友元函数:*运算符重载定义,可访问私有private内的数据 Time operator*(double m, const Time &t) { cout << "调用了友元函数" << endl; // 调用常规*运算符重载,偷懒的简便写法 return t * m; } // 友元函数:<<运算符重载,固定写法,利用友元函数可以访问类私有数据特性, // 下式实际变为cout<<"时间是:" << t._hours; // 因为返回的是ostream,所以可以连续用<<符号 // 记住即可:常用写法 ostream &operator<<(ostream &os, const Time &t) { // 利用引用符号&,实际修改了os对象的内容如下 os << "时间是:" << t._hours << ",状态成员是:" << t.mode; // 返回ostream对象os return os; } int main() { // 定义枚举变量Europe Time::Mode Europe = Time::EUROPE; Time t[3] = {Time(1), Time(2, Europe)}; // 调用*运算符重载 t[2] = t[0] * 10.0; // <<运算符重载:cout传给os,t[2]传给t cout << t[2] << endl; // 调用友元函数*运算符重载:10.0传给m,t[1]传给t t[2] = 10.0 * t[1]; // <<运算符重载:cout传给os,t[2]传给t cout << t[2] << endl; }
- 显示
调用了*重载函数 时间是:10,状态成员是:0 调用了友元函数 调用了*重载函数 时间是:20,状态成员是:1
四、类的类型转换
- 类型转换:类内默认存在隐式类型转换,explicit关键字可关闭此默认
- 代码示例
#include <iostream> using namespace std; class Time { private: int _hours; public: Time(){_hours = 0;}; //********隐式强制类型转换:关键函数*******// // 此处若声明为explicit Time(int h);则隐式强制类型转换不可用 Time(int h){_hours = h;}; ~Time(){}; Time trans(Time t) { // d、返回隐式强制类型转换: // 函数声明中返回的是Time对象,return返回的是int // 此处是Time = t._hours;,应用了a return t._hours; }; void show() const{cout << "时间是:" << _hours << endl;}; }; int main() { // 显式强制类型转换 Time t1(1.0); t1.show(); Time t2; // a、基础隐式强制类型转换: // 先将2.0格式化为2,然后传给星行的h进而赋值对象t2 t2 = 2.0; t2.show(); // b、构造隐式强制类型转换: // 先将3.0格式化为2,然后传给星行的h进而初始化对象t3 Time t3 = 3.0; t3.show(); // c、传参隐式强制类型转换: // 因为trans函数接收Time对象,隐式传参t=2,应用了a t3 = t3.trans(4); t3.show(); }