1、什么是运算符重载?
我们都知道在c++语言中,函数是允许重载的。与函数重载相似,运算符也存在重载问题,运算符重载是另一种调用函数的方法,是指同样的运算符可以施加于不同类型的操作数上面,也就是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时产生不同的行为,是一种静态联编的多态。
2、为什么要用运算符重载?
C++语言预定义的运算符只适用于基本数据类型。为了解决一些实际问题,程序员经常会定义一些新类型,即自定义类型,然而C++不允许生成新的运算符,因此为了实现对自定义类型的操作,就必须自己来编写函数说明某个运算符如何作用于这些数据类型,这样的程序可读性较差。针对这种情况,C++允许重载现有的大多数运算符,也就是允许给已有的运算符赋予新的含义,从而提高了C++的可扩展性,使得针对同样的操作,使用重载运算符比使用显式函数调用更能提高程序的可读性。
C++语言对运算符重载进行了以下规定限制:
- 只能重载C++语言中原先已定义的运算符,不能自己创造新的运算符进行重载。
- 并不是所有的运算符都可以重载。不能进行重载的运算符:”.” “.*” “::” “?:”
- 不能改变运算符原有的优先级和结合性。
- 不能改变运算符对预定义类型数据的操作方式,但是可以根据实际需要,对原有运算符进行适当的改造和扩充。
- 运算符重载有两种方式:重载为类的成员函数&重载为类的友元函数。
3、运算符重载基本定义方法:
class 类名
{
...
返回类型 operator 运算符(形参表) ;
};
//在类外定义成员运算符函数的格式:
返回类型 类名::operator 运算符(形参表)
{
//函数体
}
- 返回类型是指运算符重载函数的运算结果类型;
- operator是定义运算符重载函数的关键字;
- 运算符是要重载的运算符名称;
- 形参表中给出了重载运算符所需要的参数和类型。
4、成员函数重载运算符实例:
- 单目运算符在进行重载时,函数没有接收参数,运算符后的操作数有this指针指出。
- 双目运算符在进行重载时,左边操作数是访问该重载运算符的对象本身的数据,有this指针指向,右边操作数通过成员运算符函数的参数指出。所以,此时成员运算符函数只有一个参数。
运用运算符重载,模拟string容器的部分功能:
#include<iostream>
using namespace std;
class Mstring
{
public:
Mstring(const char *str);
Mstring(const Mstring& str);
Mstring(const Mstring& str1, const Mstring& str2);
~Mstring();
Mstring operator=(const Mstring& str);
Mstring operator=(const char* str);
bool operator==(const Mstring& str)const;
bool operator==(const char* str)const;//Mstring == char* char*==Mstring
bool operator!=(const Mstring &str)const;
bool operator>(const Mstring&str)const;
Mstring operator + (const Mstring& str)const;
char& operator[](int pos); //arr[4] = 2;
ostream& operator<<(ostream& out);
private:
char *_str;
friend bool operator==(const char* str, const Mstring &src);
friend ostream& operator<<(ostream& out, const Mstring& src);
};
Mstring::Mstring(const char *str = NULL)
{
if (NULL == str)
{
_str = NULL;
return;
}
_str = new char[strlen(str) + 1];
memcpy_s(_str, strlen(str) + 1, str, strlen(str) + 1);
}
Mstring::Mstring(const Mstring& str)
{
if (NULL == str._str)
{
_str = NULL;
return;
}
_str = new char[strlen(str._str) + 1];
memcpy_s(_str, strlen(str._str) + 1, str._str, strlen(str._str) + 1);
}
Mstring::Mstring(const Mstring& str1, const Mstring& str2)
{
if (str1._str == NULL && str2._str == NULL)
{
_str = NULL;
return;
}
_str = new char[strlen(str1._str) + strlen(str2._str) + 1];
memcpy_s(_str, strlen(str1._str) + 1, str1._str, strlen(str1._str) + 1);
strcat_s(_str, strlen(str1._str) + strlen(str2._str) + 1, str2._str);
}
Mstring::~Mstring()
{
if (NULL == _str)
{
return;
}
delete[]_str;
_str = NULL;
}
Mstring Mstring::operator=(const Mstring& str)
{
if (this == &str)
{
return *this;
}
if (NULL != _str)
{
delete[]_str;
}
if (NULL == str._str)
{
_str = NULL;
return *this;
}
_str = new char[strlen(str._str) + 1];
memcpy_s(_str, strlen(str._str) + 1, str._str, strlen(str._str) + 1);
return _str;
}
Mstring Mstring::operator=(const char* str)
{
if (NULL != _str)
{
delete[]_str;
}
if (NULL == str)
{
_str = NULL;
return *this;
}
_str = new char[strlen(_str) + 1];
memcpy_s(_str, strlen(_str) + 1, _str, strlen(_str) + 1);
return _str;
}
bool Mstring::operator==(const Mstring& str)const
{
return strcmp(_str, str._str) == 0;
}
bool Mstring::operator==(const char* str)const//Mstring == char* char*==Mstring
{
return strcmp(_str, str) == 0;
}
bool Mstring::operator!=(const Mstring &str)const
{
return !strcmp(_str, str._str) == 0;
}
bool Mstring::operator>(const Mstring&str)const
{
return strcmp(_str, str._str) > 0;
}
Mstring Mstring::operator+(const Mstring& str)const
{
return Mstring(*this, str);
}
char& Mstring::operator[](int pos) //arr[4] = 2;
{
return _str[pos];
}
ostream& Mstring::operator<<(ostream& out)
{
out << _str << endl;
return out;
}
bool operator==(const char* str, const Mstring &src)
{
return strcmp(str, src._str) == 0;
}
ostream& operator<<(ostream& out, const Mstring& src)
{
out << src._str << endl;
return out;
}
int main()
{
//测试赋值运算符“=”、"+"
Mstring str1 = "hello";
Mstring str2 = str1;
Mstring str3 = "wrold";
Mstring str4 = str2 + str3;
str1 = str1.operator+("sss");
cout << str1 << str2 << str3 << str4 << endl;
//测试判等和不等运算符“==”、“!=”
if (str1 == str2 && str2 == str3)
{
cout << "str1 == str2 == str3" << endl;
}
else
{
if (str1 != str2)
{
cout << "str1 != str2 ,str2 == str3" << endl;
}
else if (str2 != str3)
{
cout << "str1 == str2,str2 != str3" << endl;
}
else
{
cout << "str1 != str2,str2 != str3" << endl;
}
}
//测试下标运算符"[]"
char s = str1[0];
cout << s << endl;
return 0;
}
5、友元函数重载运算符实例
大多数情况下用友元函数或用成员函数重载运算符,在功能上是没有差别的。用友元函数重载运算符时,因为友元运算符函数没有this指针,所以如果是单目运算符时,则参数表中有一个操作数,如果是双目运算符时,则参数表中有两个操作数。
friend 函数类型 operator 重载的运算符 ( 形参 ) //单目运算符重载
{...}
friend 函数类型 operator 重载的运算符 ( 形参1 , 形参2 ) //双目运算符重载
{...}
友元函数重载复数的“+” “-"操作。
#include <iostream>
using namespace std;
//复数
class Complex
{
private:
double real, imag;
public:
Complex(double r = 0.0, double i = 0.0);
void Print();
friend Complex operator+(Complex a, Complex b);
friend Complex operator-(Complex a, Complex b);
};
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex operator+(Complex a, Complex b)
{
Complex temp;
temp.real = b.real + a.real;
temp.imag = b.imag + a.imag;
return temp;
}
Complex operator-(Complex a, Complex b)
{
Complex temp;
temp.real = b.real - a.real;
temp.imag = b.imag - a.imag;
return temp;
}
void Complex::Print()
{
cout << real;
if (imag > 0)
cout << "+";
if (imag != 0)
cout << imag << "i" << endl;
}
int main()
{
Complex c1(1.1, 2.2), c2(3.3, 4.4), total;
total = c1 + c2;
total.Print();
total = c1 - c2;
total.Print();
return 0;
}
6、成员函数重载运算符和友元函数重载运算符比较
- 单目运算符,成员函数重载运算符不带参数,而友元函数重载运算符带有一个参数。
- 双目运算符,成员函数重载运算符带有一个参数,而友元函数重载运算符带有两个参数;
- 双目运算符一般可以被重载为友元运算符或成员运算符函数,下面的情况必须使用友元函数。
例:
class Complex
{
private:
double real, imag;
public:
Complex(double r = 0.0, double i = 0.0);
void Print();
Complex operator+(Complex a);
};
如果类Complex的对象com要做赋值运算和加法运算,下面的语句时正确的。
com = com + 10;
这是因为对象com是运算符”+”的做操作数,在调用重载运算符”+”的函数时,this指针指向com。而下面的语句就不正确了。
com = 10 + com;
这是因为左操作数是一个整数,而整数是一个内部数据类型,不能产生对成员运算符函数的调用。解决这类问题的方法是采用两个友元函数来重载运算符函数”+”,从而消除运算符”+”的左操作数是内部数据类型而带来的问题。
一般而言,对于双目运算符,将它重载为友元运算符函数比重载为成员运算符函数便于使用。如果一个运算符的操作需要修改类对象的状态,建议使用成员运算符函数;如果运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则运算符必须用友元函数,而不能用成员函数。
- 对于单目运算符,建议选择成员函数;
- 对于运算符=、()、[],建议选择成员函数;
- 对于运算符+=、-=、/=、*=、&=、!=、~=、%=、>>=、<<=,建议重载为成员函数。
- 其他运算符,建议重载为友元函数。