C++中重载操作符能够扩展操作符的功能,操作符重载是以函数的方式进行的。其实操作符重载的本质为用特殊形式的函数扩展操作符的功能。
重载操作符时是通过operator关键字来定义特殊的函数,其本质是通过函数重载操作符。其语法规则如下:
Type operator Sign(const Type& p1, const Type& p2)
{
Type ret;
return ret;
}
Sign为系统中预定义的操作符,如:+、-、*、/,等
操作符重载函数可以是全局函数,也可以是成员函数,下面分别给出实现:
1.重载为全局函数
重载为全局函数时,必须给出所有的操作数。当然,全局函数也可以作为友元函数。下面代码为通过友元函数重载复数类的“+”操作符:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2; // operator + (c1, c2)
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}
运行结果如下:
下面给出复数类“==”和“!=”操作符非友元全局函数重载:
#include <iostream>
using namespace std;
class Complex
{
double m_a;
double m_b;
public:
Complex(double a = 0, double b = 0)
{
m_a = a;
m_b = b;
}
double getA() const
{
return m_a;
}
double getB() const
{
return m_b;
}
};
bool operator == (const Complex& l, const Complex& r)
{
return (l.getA() == r.getA()) && (l.getB() == l.getB());
}
bool operator != (const Complex& l, const Complex& r)
{
return !(l == r);
}
int main()
{
Complex c1(1, 2);
Complex c2(1, 2);
cout << "(c1 == c2) : " << (c1 == c2) << endl;
cout << "(c1 != c2) : " << (c1 != c2) << endl;
return 0;
}
运行结果如下所示:
2.重载为成员函数
操作符重载为成员函数时,比重载为全局操作符重载函数少一个参数(左操作数为this指针),编译器优先在成员函数中寻找操作符重载函数。下面给出复数类各操作符重载为成员函数的代码:
Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
double a;
double b;
public:
Complex(double a = 0, double b = 0);
double getA();
double getB();
double getModulus();
Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
bool operator == (const Complex& c);
bool operator != (const Complex& c);
Complex& operator = (const Complex& c);
};
#endif
Complex.cpp
#include "Complex.h"
#include "math.h"
Complex::Complex(double a, double b)
{
this->a = a;
this->b = b;
}
double Complex::getA()
{
return a;
}
double Complex::getB()
{
return b;
}
double Complex::getModulus()
{
return sqrt(a * a + b * b);
}
// 重载+操作符
Complex Complex::operator + (const Complex& c)
{
double na = a + c.a;
double nb = b + c.b;
Complex ret(na, nb);
return ret;
}
// 重载-操作符
Complex Complex::operator - (const Complex& c)
{
double na = a - c.a;
double nb = b - c.b;
Complex ret(na, nb);
return ret;
}
// 重载*(乘法)操作符
Complex Complex::operator * (const Complex& c)
{
double na = a * c.a - b * c.b;
double nb = a * c.b + b * c.a;
Complex ret(na, nb);
return ret;
}
// 重载/(除法)操作符
Complex Complex::operator / (const Complex& c)
{
double cm = c.a * c.a + c.b * c.b;
double na = (a * c.a + b * c.b) / cm;
double nb = (b * c.a - a * c.b) / cm;
Complex ret(na, nb);
return ret;
}
// 重载==操作符
bool Complex::operator == (const Complex& c)
{
return (a == c.a) && (b == c.b);
}
// 重载!=操作符
bool Complex::operator != (const Complex& c)
{
return !(*this == c);
}
// 重载=(赋值)操作符
Complex& Complex::operator = (const Complex& c)
{
if( this != &c )
{
a = c.a;
b = c.b;
}
return *this;
}
main.cpp
#include <stdio.h>
#include "Complex.h"
int main()
{
Complex c1(1, 2);
Complex c2(3, 6);
Complex c3 = c2 - c1;
Complex c4 = c1 * c3;
Complex c5 = c2 / c1;
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
Complex c6(2, 4);
printf("c3 == c6 : %d\n", c3 == c6);
printf("c3 != c4 : %d\n", c3 != c4);
(c3 = c2) = c1;
printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
return 0;
}
运行结果如下:
1.C++规定,赋值操作符“=”只能重载为成员函数;
2.操作符重载不能改变原操作符的优先级;
3.操作符重载不能改变操作数的个数;
4.操作符重载不能改变操作符的原有语义。