C++裏面有一個叫作運算符重載的特性,它其實是基於函數實現的,下面就來介紹一下運算符重載。
1、What
C++中預定義的運算符的操作對象只能是基本數據類型。但實際上,對於許多用戶自定義類型(例如類),也需要類似的運算操作。這時就必須在C++中重新定義這些運算符,賦予已有運算符新的功能,使它能夠用於特定類型執行特定的操作。運算符重載的實質是函數重載,它提供了C++的可擴展性,也是C++最吸引人的特性之一。
運算符重載函數的定義與其他函數的定義類似,惟一的區別是運算符重載函數的函數名是由關鍵字operator和其後要重載的運算符符號構成的。運算符函數定義的一般格式如下:
返回值 opreator 運算符(參數列表)
{
函數體
}
當然,並不是所有運算符都可以實現重載,下面列舉了能實現重載和不能實現重載的運算符。
2、Notice
a. 不能通過連接其他符號來創建新的操作符:比如operator@;
b. 重載操作符必須有一個類類型或者枚舉類型的操作數;
int operator +(const int _iNum1 , const int _iNum2 ) // 報錯
{
return ( _iNum1 + _iNum2);
}
typedef enum TEST {one ,two ,three };
int operator+(const int _iNum1 , const TEST _test ) //正確
{
return _iNum1;
}
c. 用於內置類型的操作符,其含義不能改變,例如:內置的整型+,不能改變其含義;
d. 不再具備短求值特性
重載操作符不能保證操作符的求值順序,在重載&&和||中,對每個操作數都要進行求值,而且對操作數的求值順序不能做規定,因此:重載&&、||和逗號操作符不是好的做法。
e. 作爲類成員的重載函數,其形參看起來比操作數數目少1
成員函數的操作符有一個默認的形參this,限定爲第一個形參。
CTest operator+(const CTest test1, const CTest test2)const // 報錯
{
return test1;
}
CTest operator+(const CTest test1)const
{
return test1;
}
f. 一般將算術操作符定義爲非成員函數,將賦值運算符定義成員函數
g. 操作符定義爲非類的成員函數時,一般將其定義爲類的友元
h. == 和 != 操作符一般要成對重載
i.下標操作符[]:一個非const成員並返回引用,一個是const成員並返回引用
j. 解引用操作符*和->操作符,不顯示任何參數
k. 自增自減操作符
前置式++/–必須返回被增量或者減量的引用
後綴式操作符必須返回舊值,並且應該是值返回而不是引用返回
l. 輸入操作符>>和輸出操作符<<必須定義爲類的友元函數
3、Realize
通過一個複數類來深入認識運算符重載
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
//實現複數類
class Complex
{
public:
Complex(const double real = 0.0, const double image = 0.0)
:_real(real)
, _image(image)
{
cout << "Create:" << this << endl;
}
Complex(const Complex& complex)
:_real(complex._real)
, _image(complex._image)
{
cout << "Copy Create:" << this << endl;
}
~Complex()
{
cout << "Destroy:" << this << endl;
}
void Print()
{
cout << "real:" << _real << " image:" << _image << endl;
}
//實現各個運算符的重載
//= + - * /
Complex& operator=(const Complex& complex)
{
if (this != &complex)
{
_real = complex._real;
_image = complex._image;
}
return *this;
}
Complex operator+(const Complex& complex)
{
return Complex(_real + complex._real, _image + complex._image);
}
Complex operator-(const Complex& complex)
{
return Complex(_real - complex._real, _image - complex._image);
}
Complex operator*(const Complex& complex)
{
//複數乘法:(a+bi) * (c+di) = (ac-bd)+ (ad+bc)i
return Complex((_real*complex._real - _image*complex._image), (_real*complex._image + _image*complex._real));
}
Complex operator/(const Complex& complex)
{
//複數除法:(a+bi) / (c+di) = (ac+bd)/(c*c+d*d) + (bc-ad)i/(c*c+d*d)
return Complex((_real*complex._real + _image*complex._image) / (complex._real*complex._real + complex._image*complex._image), \
(_image*complex._real - _real*complex._image) / (complex._real*complex._real + complex._image*complex._image));
}
//+= -= *= /=
Complex operator+=(const Complex& complex)
{
_real += complex._real;
_image += complex._image;
return *this;
}
Complex operator-=(const Complex& complex)
{
_real -= complex._real;
_image -= complex._image;
return *this;
}
Complex operator*=(const Complex& complex)
{
//因爲下面的運算中用到的都是_real和_image的初始值,所以在這兒先把它們的初始值保存下來(temp2可以不用定義直接用_image)
double temp1 = _real;
double temp2 = _image;
_real = temp1*complex._real - temp2*complex._image;
_image = temp1*complex._image + temp2*complex._real;
return *this;
}
Complex operator/=(const Complex& complex)
{
double temp1 = _real;
double temp2 = _image;
_real = (temp1*complex._real + temp2*complex._image) / (complex._real*complex._real + complex._image*complex._image);
_image = (temp2*complex._real - temp1*complex._image) / (complex._real*complex._real + complex._image*complex._image);
return *this;
}
//> >= < <= == !=
bool operator>(const Complex& complex)
{
return _real > complex._real;
}
bool operator>=(const Complex& complex)
{
return _real >= complex._real;
}
bool operator<(const Complex& complex)
{
return _real < complex._real;
}
bool operator<=(const Complex& complex)
{
return _real <= complex._real;
}
//上面四個函數只比較實部是因爲:複數實部不爲0,那麼此時它是一個虛數,無法比較大小,這兒只是爲了說明這幾個運算符的
//重載是如何實現的,沒有實際的邏輯意義
bool operator ==(const Complex& complex)
{
return (_real == complex._real) && (_image == complex._image);
}
bool operator !=(const Complex& complex)
{
return (_real != complex._real) || (_image != complex._image);
}
//前置++ --
Complex& operator++()
{
_real++;
_image++;
return *this;
}
Complex& operator--()
{
_real--;
_image--;
return *this;
}
//後置++ --
Complex operator++(int)
{
//因爲後置的++和--,是先賦值,再++或--,所以這兒用一個臨時變量來記住變量的值,然後把該變量自加1,
//此時變量的值已經變了,但因爲返回的是變量自加之前的值,所以整個表達式的值是該變量自加前的值
Complex temp(*this);
_real++;
_image++;
return temp;
}
Complex operator--(int)
{
Complex temp(*this);
_real--;
_image--;
return temp;
}
//邏輯運算 && || !
bool operator&&(const Complex& complex)
{
return ((_real != 0 || _image != 0) && (complex._real != 0 || complex._image != 0));
}
bool operator||(const Complex& complex)
{
return ((_real != 0 || _image != 0) || (complex._real != 0 || complex._image != 0));
}
bool operator!()
{
return !(_real != 0 || _image != 0);
}
private:
double _real;
double _image;
};
//Test Complex() / Complex(const Complex& complex) / ~Complex() / Print()
void TestFun1()
{
Complex c1;
Complex c2(1.1, 2.2);
Complex c3(c2);
c1.Print();
c2.Print();
c3.Print();
}
//Test operator= / operator+ / operator- / operator* / operator/
void TestFun2()
{
Complex c1(1.1, 2.2);
Complex c2(1.1, 2.2);
Complex c3(0.0, 0.0);
c3 = c2;
Complex c4 = c1 + c2;
Complex c5 = c1 - c2;
Complex c6 = c1 * c2;
Complex c7 = c1 / c2;
c1.Print();
c2.Print();
c3.Print();
c4.Print();
c5.Print();
c6.Print();
c7.Print();
}
//Test operator+= / operator-= / operator*= / operator/=
void TestFun3()
{
Complex c1(1.1, 2.2);
Complex c2(0.0, 0.0);
Complex c3(0.0, 0.0);
Complex c4(1.0, 1.0);
Complex c5(1.1, 2.2);
c2 += c1;
c3 -= c1;
c4 *= c1;
c5 /= c1;
c1.Print();
c2.Print();
c3.Print();
c4.Print();
c5.Print();
}
//Test operator> / operator>= / operator< / operator<= / operator== / operator!=
void TestFun4()
{
Complex c1(1.1, 2.2);
Complex c2(0.0, 0.0);
bool b1 = c1 > c2;
bool b2 = c1 >= c2;
bool b3 = c1 < c2;
bool b4 = c1 <= c2;
bool b5 = c1 == c2;
bool b6 = c1 != c2;
cout << b1 << ' ' << b2 << ' ' << b3 << ' ' << b4 << ' ' << b5 << ' ' << b6 << endl;
}
//Test operator++() / operator++(int) / operator--() / operator--(int)
void TestFun5()
{
Complex c1(1.1, 2.2);
Complex c2(1.1, 2.2);
Complex temp(0.0, 0.0);
//因爲要是直接用c1或c2輸出,那麼在輸出時c1、c2的值已經是++或--運算的下一次使用了,因此用temp記住使用前的值
temp = ++c1;
temp.Print();
temp = c1++;
temp.Print();
temp = --c2;
temp.Print();
temp = c2--;
temp.Print();
}
//Test operator&& / operator|| / operator!
void TestFun6()
{
Complex c1(1.1, 2.2);
Complex c2(0.0, 0.0);
bool b1 = c1 && c2;
bool b2 = c1 || c2;
bool b3 = !c2;
cout << b1 << ' ' << ' ' << b2 << ' ' << b3 << endl;
}
int main()
{
//TestFun1();
//TestFun2();
TestFun3();
//TestFun4();
//TestFun5();
//TestFun6();
getchar();
return 0;
}