游戏开发之运算符重载(C++基础)

1,632 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

游戏开发之运算符重载(C++基础)

① 运算符重载就是对已有的操作规则重写定义,赋予其另一种功能,以适应不同的数据类型。 ②运算符重载(operator overloading)只是一种“语义上的方便”,也就是它只是另一种函数调用的方式,其本质还是对函数的的调用。

1.重载函数运算符

重载的函数运算符关联友元函数。(友元函数是一个全局函数,只是友元函数可以访问某个类的私有成员变量。) 重载函数运算符的语法:数据类型 operator 运算符(参数列表){函数体}

	class A
	{
		//运算符重载关联友元函数
		friend std::istream& operator>>(std::istream& input, A &a);
		friend std::ostream& operator<<(std::ostream& output, const A &a);
		friend A operator+(const A& a1, const A& a2);
		//由于B类是在A类定义后才定义的,所以需要添加class关键字,告诉编译器在编译的时候是可以找到它的定义的,这样才不会报错。
		friend A operator+(const A& a, const class B& b);
	private:
		int a;
	};


	class B
	{
		//运算符重载关联友元函数
		friend std::istream& operator>>(std::istream& input, B &b);
		friend A operator+(const A& a, const B& b);
	private:
		int b;
	};

	//重载输入流
	// istream 输入流,字节流从输入设备流向内存。
	std::istream& operator>>(std::istream& input, A &a)
	{
		input >> a.a;
		return input;
	}


	//重载输出流
	//ostream 输出流,字节流从内存流向输出设备。
	std::ostream& operator<<(std::ostream& output,const A &a)
	{
		output << "A::a " << a.a << std::endl;
		return output;
	}

	//重载+运算符
	A operator+(const A& a1, const A& a2)
	{
		A a;
		a.a = a1.a + a2.a;
		return a;
	}


	// istream 输入流,字节流从输入设备流向内存。
	std::istream& operator>>(std::istream& input, B &b)
	{
		input >> b.b;
		return input;
	}

	//重载+运算符
	A operator+(const A& a, const B& b)
	{
		A a1;
		a1.a = a.a + b.b;
		return a1;
	}

	int main()
	{
		//调用方式
		A a1, a2;
		B b1;
		std::cin >> a1 >> a2 >> b1;
		std::cout << a1 << a1 + a2 << a1 + b1;
		return 0;
	}

2.重载类中的运算符

在类中声明及定义的语法:数据类型 operator 运算符(参数列表){函数体} 类内的重载运算符受权限约束。 函数运算符重载有2个(friend 声明及类外定义)且还需要考虑类对象。 而类内的重载运算符不需要考虑类本身。

	class A
	{
	private:
		int a = 0;
	public:
		//运算符重载受权限约束
		//重载+=运算符
		A& operator +=(const A &a)
		{
			std::cout << "A" << std::endl;
			this->a += a.a;
			return *this;
		}
		
		//重载+=运算符
		A& operator+=(const int a)
		{
			std::cout << "int" << std::endl;
			this->a += a;
			return *this;
		}

		//重载前置++和后置++
		A& operator++()
		{
			this->a += 1;
			return *this;
		}

		A operator++(int)
		{
			A a1;
			a1.a = this->a;
			this->a++;
			return a1;
		}

		//重载*运算符
		int operator*() const
		{
			//返回实体
			return this->a;
		}

		//重载->运算符
		A* operator->()
		{
			return this;
		}

		void Print() const
		{
			std::cout << "Print" << std::endl;
		}

		//重载=运算符
		A& operator=(A& a)
		{
			this->a = a->a;
			return *this;
		}
	};

	int main()
	{
		//访问方式(调用方式)
		A a;
		a += a += 1;//+=从右往左执行
		++a;
		a++;
		std::cout << *a << std::endl;

		a->Print();
		std::cout << *a << std::endl;

		A a1, a2;
		a1 = a2 = a;
		std::cout << *a1 << " " << *a2 << std::endl;
		return 0;
	}

3.类内函数调用符号()的重载

声明语法:类名& operator()(参数列表){赋值操作;return *this;} 如:类名& operator()(int x,int y){ 赋值操作; return *this;} 类外访问方式: 类名 变量名; 类名 a = 变量名(10,20);

4.不要重载&&、||运算符

会影响正常的表达式判断,不便于其它人员阅读,歧义很大。

5.可以重载的运算符及不可重载的运算符

在C++中几乎所有的运算符都可以重载,但是重载之前我们需要考虑是否会发生灾难性的编程事故,如:有人错误理解重载,总会有阅读别人代码的时候。

效果图

6.运算符重载总结

①=,[],和->操作运算符只能通过类内成员函数进行重载。 ②<<和>>只能通过全局函数关联友元函数进行重载。 ③不要重载&&和||操作符,因为无法实现短路规则。 版本声明:本文为CSDN博主[ufgnix0802]的原创文章。
原文链接:(blog.csdn.net/qq135595696…)