作者:gzshun. 原创作品,转载请标明出处!
来源:http://blog.csdn.net/gzshun
运算符重载就是赋予已有的运算符多重含义。C++中通过重新定义运算符,使它能够用于特定类的对象执行特定的功能,从而增强了C++的扩充能力。
一、运算符重载的特点
在实现运算符重载的方法有2种:
1.用类的成员函数来实现;
2.通过类的友元函数来实现。
一个很重要的特点:
1.用类的成员函数来实现
成员函数的形参个数 = 几目运算符 - 1
假设重载运算符=,+,或+=,这些运算符是双目运算符,所以类的成员函数的形参必须为1.
2.通过类的幽怨函数来实现
友元函数的形参个数 = 几目运算符
假设重载运算符=,+,或+=,这些运算符是双目运算符,所以友元函数的形参必须为2.
二、C++不允许重载的运算符
不是所有的运算符都可以重载。在C++中不允许重载的运算符有:三目运算符"?:",成员操作符"."、成员指针操作符"*"、作用域操作符"::"以及sizeof运算符。
三、运算符重载的基本用法-例子(+, -, +=)
#include <iostream> using namespace std; class CComplex { public: CComplex(double r = 0, double i = 0) : mRealPart(r), mImagePart(i) { } void Print() const { cout << mRealPart << " + " << mImagePart << "i" << endl; } CComplex operator+(CComplex &c); CComplex operator+(double r); friend CComplex operator+(double r, CComplex &c); friend CComplex operator-(CComplex &c); friend CComplex operator-(CComplex &c, double r); CComplex operator-(CComplex &c); void operator+=(CComplex &c); private: double mRealPart; double mImagePart; }; //类的成员函数 重载双目运算符"+" CComplex CComplex::operator+(CComplex &c) { cout << "调用CComplex::operator+(CComplex &c)函数" << endl; CComplex tmp; tmp.mRealPart = this->mRealPart + c.mRealPart; tmp.mImagePart = this->mImagePart + c.mImagePart; return tmp; } //类的成员函数 重载双目运算符"+" CComplex CComplex::operator+(double r) { cout << "调用CComplex::operator+(double r)函数" << endl; CComplex tmp; tmp.mRealPart = this->mRealPart + r; tmp.mImagePart = this->mImagePart; return tmp; } //类的友元函数 重载双目运算符"+" CComplex operator+(double r, CComplex &c) { cout << "调用friend CComplex operator+(double r, CComplex &c)函数" << endl; CComplex tmp; tmp.mRealPart = r + c.mRealPart; tmp.mImagePart = c.mImagePart; return tmp; } //类的友元函数 重载单目运算符"-" CComplex operator-(CComplex &c) { cout << "调用friend CComplex operator-(CComplex &c)函数" << endl; return CComplex(-c.mRealPart, -c.mImagePart); } //类的友元函数 重载双目运算符"-" CComplex operator-(CComplex &c, double r) { cout << "调用friend CComplex operator-(CComplex &c, double r)函数" << endl; CComplex tmp; tmp.mRealPart = c.mRealPart - r; tmp.mImagePart = c.mImagePart; return tmp; } //类的成员函数 重载双目运算符"-" CComplex CComplex::operator-(CComplex &c) { cout << "调用CComplex operator-(CComplex &c)函数" << endl; CComplex tmp; tmp.mRealPart = this->mRealPart - c.mRealPart; tmp.mImagePart = this->mImagePart - c.mImagePart; return tmp; } //类的成员函数 重载双目运算符"+=" void CComplex::operator+=(CComplex &c) { cout << "调用void operator+=(CComplex &c)函数" << endl; this->mRealPart += c.mRealPart; this->mImagePart += c.mImagePart; } int main() { CComplex c1(11, 12); CComplex c2(13, 14); CComplex c; c = c1 + c2; c.Print(); c = c1 + 6.0; c.Print(); c = 8.0 + c2; c.Print(); c = -c1; c.Print(); c = c1 - 6.0; c.Print(); c = c2 - c1; c.Print(); c1 += c2; c1.Print(); return 0; }
执行结果:
调用CComplex::operator+(CComplex &c)函数
24 + 26i
调用CComplex::operator+(double r)函数
17 + 12i
调用friend CComplex operator+(double r, CComplex &c)函数
21 + 14i
调用friend CComplex operator-(CComplex &c)函数
-11 + -12i
调用friend CComplex operator-(CComplex &c, double r)函数
5 + 12i
调用CComplex operator-(CComplex &c)函数
2 + 2i
调用void operator+=(CComplex &c)函数
24 + 26i
四、自增与自减的重载
#include <iostream> using namespace std; class CCounter { public: CCounter(int x = 0) : mCounter(x) { } CCounter operator++(); CCounter operator++(int); void Print() const { cout << "mCounter = " << mCounter << endl; } private: int mCounter; }; CCounter CCounter::operator++() { cout << "调用CCounter operator++()函数 前缀自增" << endl; ++mCounter; return *this; } CCounter CCounter::operator++(int) { cout << "调用CCounter operator++(int)函数 后缀自增" << endl; mCounter++; return *this; } int main() { int i; CCounter demo1, demo2; for(i = 0; i < 2; i++) { ++demo1; } demo1.Print(); for(i = 0; i < 2; i++) { demo2++; } demo2.Print(); return 0; }
执行结果:
调用CCounter operator++()函数 前缀自增
调用CCounter operator++()函数 前缀自增
mCounter = 2
调用CCounter operator++(int)函数 后缀自增
调用CCounter operator++(int)函数 后缀自增
mCounter = 2