c++不允许用户定义新的运算符,只能对于已有的运算符进行重载。除了5个不能外
.成员访问运算符 , .*成员访问指针运算符 , ::域运算符 ,sizeof尺寸运算符 , ?:条件运算符
其余都可以
运算符重载函数可以作为类的成员函数,也可以放在类外作为类的友元函数
例:
#include <iostream>
//实现复数加法
#define FLAG 0
class Complex
{
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(int a,int b)
{
real = a;
imag = b;
}
// Complex ComplexAdd(Complex &x);
// Complex operator+(Complex &x);//friend Complex operator+(Complex &x);
friend Complex operator+(Complex &x ,Complex &y);
void print();
private:
int real;//实部
int imag;//复部
};
#if FLAG
Complex Complex::ComplexAdd(Complex &x)
{
Complex c;
c.real = this->real + x.real; //c.real = real + x.real;
c.imag =this->imag + x.imag;// c.imag =imag + x.imag;
return c;
}
Complex Complex::operator+(Complex &x)
{
Complex c;
c.real = this->real + x.real; //c.real = real + x.real;
c.imag =this->imag + x.imag;// c.imag =imag + x.imag;
return c;
}
#else
//Complex operator+(Complex &x)为友元函数时
Complex operator+(Complex &x,Complex &y)
{
return Complex(x.real + y.real , x.imag + y.imag);
}
#endif
void Complex::print()
{
std::cout << real << "+" << imag << "\n" << std::endl;
}
int main()
{
Complex aa(3,2),bb(5,4),cc,dd;
// cc = aa.ComplexAdd(bb);
// cc.print();
dd = aa + bb; //编译系统将其翻译成aa.operator+(bb)
dd.print();
return 0;
}
拓展:
重载运算符不能对指针单独操作
因为系统已经定义了两个指针的运算,所以你不能改变它
比如两个同类型的指针相加,就是指针的指相加,你是不能改变这个行为的
#include <iostream>
using namespace std;
class Test{
int val;
public:
Test():val(0){}
Test(int v):val(v){}
Test& operator *= (const Test *src)
{
//成员函数,有隐含的对象引用,不算单独操作指针
val *= src->val;
return *this; }
friend Test operator * (const Test &src1, const Test *src2)
{
//一个引用,一个指针,OK
Test ret;
ret.val = src1.val * src2->val;
return ret;
}
#if 0 //Error: must have an argument of class or enumerated type
friend Test operator * (const Test *src1, const Test *src2)
{ //友元,都是指针,尝试单独操作...
Test ret; ret.val = src1->val * src2->val;
return ret;
}
#endif
void print()
{ cout<<val<<endl;
}
};
int main()
{ Test test1(2), test2(3), test3;
test1.print();
test2.print();
test1 *= &test2;
test1.print();
test3 = test1 * &test2;
test3.print(); //test3 = &test1 * &test2;
return 0;
}
实例程序:
#include <iostream>
//复数的加减乘除重载
// <<运算符重载 插入器
// >>提取器
class Rational
{
public:
Rational(int num,int denom);
Rational operator+(Rational rhs);
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
friend std::ostream& operator<<(std::ostream& os,Rational f);
private:
int numerator;//分子
int denominator;//分母
void normalize();//对分数的最简化
};
Rational::Rational(int num,int denom)
{
numerator = num;
denominator = denom;
normalize();
}
void Rational::normalize()
{
if (denominator < 0)
{
numerator = -numerator;
denominator = - denominator;
}
//欧几里得算法求最大公约数
int a = abs(numerator);
int b = abs(denominator);
while (b > 0)
{
int t = a%b;
a = b;
b = t;
}
numerator /= a;
denominator /= a;
}
Rational Rational::operator+(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a* d +c*b;
int f = b*d;
return Rational(e,f);
}
Rational Rational::operator-(Rational rhs)
{
rhs.numerator = -rhs.numerator;
return operator+(rhs);
}
Rational Rational::operator*(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*c;
int f = b*d;
return Rational(e,f);
}
Rational Rational::operator/(Rational rhs)
{
int t = rhs.numerator;
rhs.numerator = rhs.denominator;
rhs.denominator = t;
return operator*(rhs);
}
std::ostream& operator<<(std::ostream& os,Rational f)
{
os << f.numerator << "/" << f.denominator;
return os;
}
int main()
{
Rational f1(1,2);
Rational f2(2,3);
//Rational c = Rational(3,4);
std::cout << f1 << "+" << f2 << "==" << (f1 + f2) << "\n";
std::cout << f1 << "-" << f2 << "==" << (f1 - f2) << "\n";
std::cout << f1 << "*" << f2 << "==" << (f1 * f2) << "\n";
std::cout << f1 << "/" << f2 << "==" << (f1 / f2) << "\n";
return 0;
}