运算符重载
同一个运算符有不同的功能
运算符重载相当于定义一个函数,当遇到该运算符时,编译器会自动调用这个函数
本质上是函数重载
返回值类型 operator运算符名称(形参列表){/*函数体*/}
可以将operator运算符名称看做函数名
原型:operator op(param-list)
return_val=obj1 (op) obj2
会被编译成
return_val=obj1.op(obj2)
注意运算符的作用域、有效域
检测运算符左右两边的对象
类内重载 - 成员函数重载
[]
=
()
->
只能通过成员函数进行重载
另外值得注意的是:对于类内的成员函数的重载,在调用的时候注意顺序问题(objint_val可以,inv_valobj不可以)
解决的办法就是使用非成员函数的方法
例子1
#include <iostream>
using namespace std;
class complex
{
private:
double real;
double imag;
public:
complex();
complex(double real,double imag);
~complex();
// 声明运算符重载
complex operator+(const complex &A) const; // operator+()~~是一个内置函数~~ ;相当于成员函数
void print() const;
};
complex::complex(/* args */)
{
this->real=0.;
this->imag=0.;
}
complex::complex(double real,double imag)
{
this->real=real;
this->imag=imag;
}
complex::~complex()
{
}
// 实现运算符重载
complex complex::operator+(const complex &A) const // 第一个complex是返回值类型
{
/*version1*/
complex B;
B.real=this->real+A.real;
B.imag=this->imag+A.imag;
return B;
/*version2*/
// return complex(this->real+A.real,this->imag+A.imag); // 创建一个临时对象,即匿名对象
}
void complex::print() const
{
cout<<this->real<<"+"<<this->imag<<"i"<<endl;
}
int main()
{
complex val1(1.2,3.4);
complex val2(1.4,3.6);
complex val3;
val3=val1+val2; // 调用成员函数operator+() 相当于val1.operator+(val2)
val3.print();
return 0;
}
例子2
/* test.h */
#ifndef __MYTIME_H__
#define __MYTIME_H__
#include <iostream>
using namespace std;
class MyTime
{
private:
int hours;
int minutes;
public:
MyTime(/* args */);
MyTime(int h,int m=0);
~MyTime();
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
MyTime operator+(const MyTime &t) const;
void Show() const;
};
#endif
/* test.cpp */
#include "test95.h"
MyTime::MyTime()
{
hours = minutes = 0;
}
MyTime::MyTime(int h, int m)
{
hours = h;
minutes = m;
}
void MyTime::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void MyTime::AddHr(int h)
{
hours += h;
}
void MyTime::Reset(int h, int m)
{
hours = h;
minutes = m;
}
void MyTime::Show() const
{
cout << hours << " hours, " << minutes << " minutes." << endl;
}
MyTime MyTime::operator+(const MyTime &t) const
{
MyTime sum;
sum.minutes = this->minutes + t.minutes;
sum.hours = this->hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
非成员函数重载
[]
=
()
->
只能通过成员函数进行重载
// 类外
clsType operator+(const clsType &obj,const clsType &obj2)
{
clsType temp;
temp.val=obj1.val+obj2.val; // 注意私有变量和公有变量,只能使用公有变量
return temp;
}
值得注意的是,成员函数的形式和非成员函数的形式 可以发生重载
例子
// 类内成员函数重载 运算符*
Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hours*mult*60 + minutes*mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
// 类外非成员函数重载 运算符*
Time operator*(double mult, const Time &t)
{
Time result;
long totalminutes = t.hours*mult*60 + t.minutes*mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
// main
obj = obj * 1.5;
adjusted.Show();
obj = 1.5 * obj;
友元函数重载
尽量使用 类内成员函数重载的方法 或 友元函数的方法(类外非成员函数的高级用法,可以访问私有变量)
见 ‘面向对象 - 友元函数’
<< 运算符重载
只能使用友元函数的方式进行重载
void operator<<(ostream &os , clsType &obj)
{
os<<obj.val<<endl;
}
连续输出
ostream & operator<<(ostream &os , clsType &obj)
{
os<<obj.val;
return os;
}