机械设备安全管理制度,清华学生退学重考北大,盛大易购
用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载。运算符重载是静态多态性的体现。
返回值类型 operator 运算符名称 (形参表列){}
+
-
*
/
%
^
&
|
~
!
=
<
>
+=
-=
*=
/=
%=
^=
&=
|=
<<
>>
<<=
>>=
==
!=
<=
>=
&&
||
++
--
,
->*
->
()
[]
new
new[]
delete
delete[]
sizeof
、: ?
、.
、::
->
、[ ]
、( )
、=
只能以成员函数的形式重载
以运算符作为名称的函数称之为运算符函数。这种重载称为运算符重载。
+
-
为例)complex operator+(const complex &leftarg, const complex &rightarg) { return complex(leftarg.real+rightarg.real,leftarg.imag+rightarg.imag); } complex sum = leftarg + rightarg;
第五行调用函数operator+,并且将leftarg
、leftarg
作为符号函数的参数,返回值赋值给sum
,等同于sum = operator+(leftarg,rightarg);
。
class complex { public: complex operator-(const complex & arg) { return complex(this.real - arg.real, this.imag - arg.imag); } }; complex diff = leftarg - rightarg;
第九行调用函数operator-,并且将leftarg
作为符号函数的参数,返回值赋值给diff
,等同于diff = leftarg.operator-(rightarg);
。
#include<iostream> #include<string> using namespace std; enum complepart { real = 0, imag }; class complex { public: int real; int imag; public: complex() : real(0),imag(0){} complex(int r, int i) : real(r),imag(i){} complex operator-(const complex & arg)// +,-,*,/ 这四个运算符重载方法一样 { return complex(this->real - arg.real, imag - arg.imag); } friend complex operator*(const complex &leftarg, const complex &rightarg);//友元函数 bool operator==(const complex &arg)// ==,!= 这两个个运算符重载方法一样 { if(this->real == arg.real && this->imag == arg.imag) return true; else return false; } complex& operator+=(const complex &arg)// +=,-=,*=,/= 这四个运算符重载方法一样 { this->real += arg.real; this->imag += arg.imag; return *this; } friend istream& operator>>(istream &input, complex &complex); friend ostream& operator<<(ostream &output, complex &complex); complex& operator++()// ++i,--i 这两个个运算符重载方法一样 { ++this->real; ++this->imag; return *this; } complex operator++(int i)// i++,i-- 这两个个运算符重载方法一样 { complex tempcomplex = *this; ++this->real; ++this->imag; return tempcomplex; } void* operator new(size_t size) { cout << "call function void* operator new(size_t size)" << endl; void* pointer = malloc(size); return pointer; } void operator delete(void* pointer) { cout << "call function void operator delete(void* pointer)" << endl; free(pointer); } void* operator new[](size_t size) { cout << "call function void* operator new[](size_t size)" << endl; void* pointer = malloc(size); return pointer; } void operator delete[](void* pointer) { cout << "call function void operator delete[](void* pointer)" << endl; free(pointer); } void* operator new(size_t size,complex* complex,int step)//placement new { cout << "call function void* operator new(size_t size,complex* complex,int step)" << endl; return complex + step; } operator int() { return this->real; }//c->operator int() int& operator [](int i) { int errorvalue = 0; if(i == 0) return this->real; else//为了演示不要在意 return this->imag; } const int& operator[](int i) const { cout << "call function const int& operator[](int i) const" << endl; if(i == 0) return this->real; else//为了演示不要在意 return this->imag; } }; complex operator+(const complex &leftarg, const complex &rightarg)//全局重载 { return complex(leftarg.real+rightarg.real,leftarg.imag+rightarg.imag); } istream& operator>>(istream &input, complex &complex) { input >> complex.real >> complex.imag; return input; } ostream& operator<<(ostream &output, complex &complex) { output << complex.real << " " << complex.imag << " "; return output; } complex operator*(const complex &leftarg, const complex &rightarg) { return complex((leftarg.real * rightarg.real) - (leftarg.imag * rightarg.imag), (leftarg.imag * rightarg.real) - (leftarg.real * rightarg.imag)); } int main() { complex leftarg(2,2); complex rightarg; cin >> rightarg;//输入3,3 complex result = leftarg + rightarg; cout << result << endl; result = leftarg - rightarg; cout << result << endl; result = leftarg * rightarg; cout << result << endl; string str = (leftarg == rightarg)?"true":"false"; cout << str << endl; result += leftarg; cout << result << endl; cout << ++result << endl; complex resulttttt = result++; cout << resulttttt << endl; cout << result << endl; complex* pointer = new complex(1,1); cout << *pointer << endl; delete pointer; complex* pointerarray = new complex[10]; cout << pointerarray[2] << endl; new(pointerarray, 2)complex(123,321);//placement new cout << pointerarray[2] << endl; cout << (int)pointerarray[2] << endl; cout << pointerarray[2][complepart::real] << endl; cout << pointerarray[2][complepart::imag] << endl; delete[] pointerarray; const complex c_result(111,222); cout << c_result[complepart::imag] << endl; return 0; } /* 运行结果为: 3 3 5 5 -1 -1 0 0 false 2 2 3 3 3 3 4 4 call function void* operator new(size_t size) 1 1 call function void operator delete(void* pointer) call function void* operator new[](size_t size) 0 0 call function void* operator new(size_t size,complex* complex,int step) 123 321 123 123 321 call function void operator delete[](void* pointer) call function const int& operator[](int i) const 222 -------------------------------- process exited after 3.063 seconds with return value 0 请按任意键继续. . . */
+
、-
、*
、/
、%
这五个运算符均为双目运算符,重载方法相同。重载例子详见第20、24、102、116行,其中第102行的+
重载函数为全局重载,测试详见第127、129、131行。
==
、!=
、<
、>
、<=
、>=
这六个运算符均为关系运算符,重载方法相同。重载例子详见第25行,测试详见第133行。
++
、--
这两个运算符均为自增自减运算符,由于运算符的特殊性,运算符又分为前置和后置形式。重载方法两种形式不同,但是相同形式的重载方法相同。重载例子详见第40、46行,测试详见第137、138行。
new
、delete
、new[]
、delete[]
这四个运算符均为空间申请与释放运算符,重载方法相似。重载例子详见第53、59、64、70行,测试详见第141、143、144、151行。在重载空间申请运算符时除new
、new[]
这两种方式外还有一种方式叫做placement new
。重载例子详见第75行,测试详见第146行。
通常new
操作分两步:
但是若已分配好内存如:complex* pointerarray = new complex[10];
,若要在pointerarray[2]
分配的内存上创建对象则需要用placement new
来完成该操作。操作如下:new(pointerarray, 2)complex(123,321);
,完成该操作后pointerarray[2]
中的复数对象将会变为123+321i
。
>>
、<<
这两个运算符均为输入和输出运算符,重载方法相似。可以将输出运算符<<
和输入运算符>>
看作是c++对左移运算符<<
和右移运算符>>
分别进行了重载,但只能输出输入标准类型。重载例子详见第38、39行,测试详见第126、128行。
(数据类型)
运算符
(数据类型)
是强制类型转换运算符,可以将对象转换为相应的类型。
[]
运算符
[]
是下标运算符,可以将对象转换为类似数组,可以通过下标操纵对象。
成员函数重载只允许右参数的隐式转换,一般单目运算符以成员函数重载。只能重载为成员函数的运算符:=
、()
、[]
、->
等。
友元函数重载能够接受左参数和右参数的隐式转换,友员函数重载运算符常用于运算符的左右操作数类型不同的情况。一般双目运算符以友元函数重载。只能重载为友元函数的运算符:<<
、>>
等。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论