操作符重载的概念

操作符重载的概念

1、需要解决的问题

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I5vCdmqz-1632839392178)(https://i.loli.net/2021/09/28/Mc6ESpPqD8175HV.png)]

//尝试1
//30-1.cpp
#include <stdio.h>
#include <string.h>

class Complex
{
	int a;
	int b;
public:
	Complex(int a = 0, int b = 0)
	{
		this->a = a;
		this->b = b;
	}
	int getA()
	{
		return a;
	}
	int getB()
	{
		return b;
	}
	friend Complex Add(const Complex& p1, const Complex& p2);
};

Complex Add(const Complex& p1, const Complex& p2)
{
	Complex ret;

	ret.a = p1.a + p2.a;
	ret.b = p1.b + p2.b;
	
	return ret;
}


int main()
{
	Complex A(1,2);
	Complex B(3,4);
	
	Complex C = Add(A,B);

	printf("C.a = %d, C.b = %d\n",C.getA(),C.getB());
	
	return 0;
}



2、思考。。

Add函数可以解决Complex对象相加的问题,但是Complex是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同。


为什么不能让 + 操作符也支持复数相加呢?


3、操作符重载1

  • C++的重载能够扩展操作符的功能
  • 操作符的重载以函数的方式进行
  • 本质:
    • 特殊形式的函数扩展操作符的功能

4、操作符重载2

  • 通过operator关键字可以定义特殊的函数

  • operator的本质是通过函数重载操作符

  • 语法:

Type operator Sign(const Type p1,const Type p2)
{
    Type ret;
    
    return ret;
}

Sign为系统中预定义的操作符,如:+,-,*,/,等
//全局函数对操作符的重载
//30-2.CPP
#include <stdio.h>
#include <string.h>

class Complex
{
	int a;
	int b;
public:
	Complex(int a = 0, int b = 0)
	{
		this->a = a;
		this->b = b;
	}
	int getA()
	{
		return a;
	}
	int getB()
	{
		return b;
	}
	friend Complex operator + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const Complex& p2)
{
	Complex ret;

	ret.a = p1.a + p2.a;
	ret.b = p1.b + p2.b;
	
	return ret;
}


int main()
{
	Complex A(1,2);
	Complex B(3,4);
	
	Complex C =A + B;//operator + (A,B)

	printf("C.a = %d, C.b = %d\n",C.getA(),C.getB());
	
	return 0;
}

//编译运行
$ g++ test.cpp 
$ ./a.out 
C.a = 4, C.b = 6


  • 可以将操作符重载函数定义为类的成员函数

    • 比全局操作符重载函数少了一个参数(左操作数 = this参数)
    • 不需要依赖友元就可以完成操作符重载
    • 编译器优先在成员函数中寻找操作符重载函数
    • 语法:
    class Type
    {
    public:
        Type operator Sign(const Type& P)
        {
            Type ret;
            
            return ret;
        }  
    };
    
    
//成员函数对操作符的重载
//30-3.cpp
#include <stdio.h>
#include <string.h>

class Complex
{
	int a;
	int b;
public:
	Complex(int a = 0, int b = 0)
	{
		this->a = a;
		this->b = b;
	}
	int getA()
	{
		return a;
	}
	int getB()
	{
		return b;
	}
	Complex operator + (const Complex& p)
	{
		Complex ret;

		printf("Complex operator + (const Complex& p)\n");
		ret.a = this->a + p.a;
		ret.b = this->b + p.b;
		
		return ret;
	}
//	friend Complex operator + (const Complex& p1, const Complex& p2);
};
/*
Complex operator + (const Complex& p1, const Complex& p2)
{
	Complex ret;
	
	printf("Complex operator + (const Complex& p1, const Complex& p2)\n");

	ret.a = p1.a + p2.a;
	ret.b = p1.b + p2.b;
	
	return ret;
}
*/

int main()
{
	Complex A(1,2);
	Complex B(3,4);
	
	Complex C = A+B;//Complex C = A.operator + (B);
	
	printf("C.a = %d, C.b = %d\n",C.getA(),C.getB());
	
	return 0;
}

//运行
$ g++ test.cpp 
$ ./a.out 
Complex operator + (const Complex& p)
C.a = 4, C.b = 6

5、小结

  • 操作符重载是C++的强大特性之一
  • 操作符重载的本质是通过函数扩展操作符的功能
  • operator关键字是实现操作符重载的关键
  • 操作符重载遵循相同的函数重载规则
  • 全局函数成员函数都可以实现对操作符的重载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值