1、c++绝大多数运算符允许重载,不能重载的运算符只有几个:
. 成员访问运算符
. * 成员指针访问运算符
:: 作用域运算符
Sizeof 长度运算符
?: 条件运算符
2、c++不允许用户自己定义新的运算符,只能对已有运算符重载
3、**不是C++运算符,但某些程序语言将其作为指数运算符
4、重载不能改变运算符的操作对象个数,如+是双目运算符,需要两个参数
5、重载不能改变运算符原有的优先级
6、重载不能改变运算符原有的结合特性
7、运算符重载函数的参数至少应有一个是类对象获对象的引用
如int operator(int x,int y)为错误的,参数不能全是c++标准类型
#include<iostream>
using namespace std;
class complex
{
public:
double real;
double imag;
complex(double r=0,double i=0)
{
real=r;
imag=i;
}
};
complex operator+(complex co1,complex co2)//定义运算符+的重载
{
complex temp;
temp.real=co1.real+co2.real;
temp.imag=co1.imag+co2.imag;
return temp;
}
int main()
{
complex com1(1.1,2.2),com2(2.2,3.4),total1,total2;
total1=operator+(com1,com2);
total2=com1+com2;
cout<<"total1:"<<"total1.real:"<<total1.real<<",total1.imag:"<<total1.imag<<endl;
cout<<"total2:"<<"total2.real:"<<total2.real<<",total2.imag:"<<total2.imag<<endl;
return 0;
}
类的数据成员重载运算符常以以下两种方式:成员运算符重载函数和友元运算符重载函数
友元运算符重载
在类的内部定义友元运算符格式:
friend 函数类型 operator 运算符(形参表)
{
函数体
}
友元函数在类里声明,类外定义:
类中:
class x
{
…
friend 函数类型 operator 运算符(形参表)
…
}
类外:
函数类型 operator 运算符(形参表)
{
函数体
}
#include<iostream>
using namespace std;
class complex
{
public:
complex(double r=0,double i=0)
{
real=r;
imag=i;
}
void print();
friend complex operator+(complex &a,complex &b);
friend complex operator-(complex &a,complex &b);
private:
double real,imag;
};
void complex::print()
{
cout<<real;
if(imag>0)
{
cout<<"+";
}
if(imag!=0)
{
cout<<imag<<"i"<<endl;
}
}
complex operator+(complex &a,complex &b)
{
complex temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
complex operator-(complex &a,complex &b)
{
complex temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
int main()
{
complex A1(2.3,4.6),A2(3.6,5.6),A3,A4;
A3=A1+A2;
A4=A1-A2;
A1.print();
A2.print();
A3.print();
A4.print();
return 0;
}
友元运算符重载单目运算符
#include<iostream>
using namespace std;
class coord
{
public:
coord(int i=0,int j=0)
{
x=i;
y=j;
}
void print()
{
cout<<"x:"<<x<<",y:"<<y<<endl;
}
friend coord operator--(coord &obj)
{
--obj.x;
--obj.y;
return obj;
}
private :
int x,y;
};
int main()
{
coord obj(12,34);
obj.print();
--obj;
obj.print();
operator--(obj);
obj.print();
return 0;
}
注意,friend coord operator–(coord &obj),形参是对象的引用,是通过传址的方式传递参数的,函数形参的变化引起实参的变化,若去掉&符号,则函数体内对形参的修改无法传到函数体外
有的运算符不能定义为友元运算符重载,如赋值运算符“=”,下标运算符“[]”,函数调用运算符“()”等
成员运算符重载函数
双目运算符重载:成员运算符重载函数的形参表仅有一个参数,他作为运算符的右操作数,另一个操作数(左操作数)是隐含的,是该类当前的对象,它是通过this指针隐含传递给函数的
#include<iostream>
using namespace std;
class complex
{
public:
complex(double r=0,double i=0);
void print();
complex operator+(complex c);
complex operator-(complex c);
private:
double real,imag;
};
complex::complex(double r,double i)
{
real=i;
imag=i;
}
void complex::print()
{
cout<<real;
if(imag>0)
{
cout<<"+";
}
if(imag!=0)
{
cout<<imag<<"i"<<endl;
}
}
complex complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
int main()
{
complex A(2.3,4.5),B(3.5,6.7),C,D;
C=A+B;
D=A-B;
A.print();
B.print();
C.print();
D.print();
return 0;
}
*单目运算符重载:*成员运算符重载函数的参数列表中没有参数,此时当前对象作为运算符的一个操作数
include
using namespace std;
class coord
{
public:
coord(int i=0,int j=0);
void print();
coord operator++();
private:
int x,y;
};
coord::coord(int i,int j)
{
x=i;
y=j;
}
void coord::print()
{
cout<<”x:”<