c/c++语言开发共享C++基础 学习笔记五:重载之运算符重载

C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载。运算符重载是静态多态性的体现。 运算符重载的规则 1. 重载公式 返回值类型 operator 运算符名称 (形参表列){} 2. 能够重载的运算符 ` ` ` …


c++基础 学习笔记五:重载之运算符重载

什么是运算符重载

用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载。运算符重载是静态多态性的体现。

运算符重载的规则

  1. 重载公式

返回值类型 operator 运算符名称 (形参表列){}

  1. 能够重载的运算符

    + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> <<= >>= == != <= >= && || ++ -- , ->* -> () [] new new[] delete delete[]

  2. 不能重载的运算符

    sizeof: ?.::

  3. 重载不能改变运算符的优先级和结合性

  4. 重载不会改变运算符的用法

  5. 运算符重载函数不能有默认的参数

  6. 重载后的运算符必须至少有一个操作数是用户自定义的类型,以此来防止为标准类型重载运算符。

  7. 特殊的运算符

    ->[ ]( )=只能以成员函数的形式重载

运算符重载的实现原理

以运算符作为名称的函数称之为运算符函数。这种重载称为运算符重载。

具体实现(以运算符+ -为例)

1. 在全局范围内重载运算符

complex operator+(const complex &leftarg, const complex &rightarg) { 	return complex(leftarg.real+rightarg.real,leftarg.imag+rightarg.imag); } complex sum = leftarg + rightarg; 

第五行调用函数operator+,并且将leftargleftarg作为符号函数的参数,返回值赋值给sum,等同于sum = operator+(leftarg,rightarg);

2.在类中重载运算符

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 请按任意键继续. . . */  

代码分析

1.双目运算符

+-*/%这五个运算符均为双目运算符,重载方法相同。重载例子详见第20、24、102、116行,其中第102行的+重载函数为全局重载,测试详见第127、129、131行。

2.关系运算符

==!=<><=>=这六个运算符均为关系运算符,重载方法相同。重载例子详见第25行,测试详见第133行。

3.自增自减运算符

++--这两个运算符均为自增自减运算符,由于运算符的特殊性,运算符又分为前置和后置形式。重载方法两种形式不同,但是相同形式的重载方法相同。重载例子详见第40、46行,测试详见第137、138行。

4.空间申请与释放运算符

newdeletenew[]delete[]这四个运算符均为空间申请与释放运算符,重载方法相似。重载例子详见第53、59、64、70行,测试详见第141、143、144、151行。在重载空间申请运算符时除newnew[]这两种方式外还有一种方式叫做placement new。重载例子详见第75行,测试详见第146行。

placement new

通常new操作分两步:

  1. 分配内存。
  2. 若为类则调用类的构造函数创建对象。

但是若已分配好内存如:complex* pointerarray = new complex[10];,若要在pointerarray[2]分配的内存上创建对象则需要用placement new来完成该操作。操作如下:new(pointerarray, 2)complex(123,321);,完成该操作后pointerarray[2]中的复数对象将会变为123+321i

5.输入和输出运算符

>><<这两个运算符均为输入和输出运算符,重载方法相似。可以将输出运算符<<和输入运算符>>看作是c++对左移运算符<<和右移运算符>>分别进行了重载,但只能输出输入标准类型。重载例子详见第38、39行,测试详见第126、128行。

6.其它运算符

  1. (数据类型)运算符

    (数据类型)是强制类型转换运算符,可以将对象转换为相应的类型。

  2. []运算符

    []是下标运算符,可以将对象转换为类似数组,可以通过下标操纵对象。

重载运算符的形式

1.以成员函数重载运算符

成员函数重载只允许右参数的隐式转换,一般单目运算符以成员函数重载。只能重载为成员函数的运算符:=()[]->等。

2.以全局函数(友元函数)重载运算符

友元函数重载能够接受左参数和右参数的隐式转换,友员函数重载运算符常用于运算符的左右操作数类型不同的情况。一般双目运算符以友元函数重载。只能重载为友元函数的运算符:<<>>等。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/599713.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐