运算符重载


运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

加号运算符重载

作用:实现两个自定义类型的相加的运算
如果现在我们有一个类如下:

class Person
{
public:
	Person()
	{
		m_a = 10;
		m_b = 30;
	}
public:
	int m_a;
	int m_b;
};

如果我们想实现

    Person p1;
	Person p2;
	Person p3 = p1 + p2;

按普通的加法肯定是不行,这1就要使用加号运算符重载
1.成员函数重载+号

class Person
{
public:
	Person()
	{
		m_a = 10;
		m_b = 30;
	}
	//成员函数重载 +
	Person operator+(Person& p)
	{
		Person temp;
		temp.m_a = this->m_a + p.m_a;
		temp.m_b = this->m_b + p.m_b;
		return temp;
	}

public:
	int m_a;
	int m_b;
}

本质:

      Person p3 = p1 + p2;
本质:Person p3 = p1.operator+(p2);

2.全局函数重载

Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_a = p1.m_a + p2.m_a;
	temp.m_b = p1.m_b + p2.m_b;
	return temp;
}

本质:

      Person p3 = p1 + p2;
本质:Person p3 = operator+(p1, p2);

运算符重载函数也是可以发生重载的

左移运算符重载

作用:输出自定义类型
比如:
先准备一个类

#include<iostream>
using namespace std;
class Person
{
public:
	Person()
	{
		m_a = 10;
		m_b = 10;
	}
	int m_a;
	int m_b;
};

如果我们想实现

Person p;
cout<<p<<endl;

我们就需要利用左移运算符重载

利用全局函数重载左移运算符

ostream& operator<<(ostream &cout, Person p)
{
	cout << "m_a= " << p.m_a << "   m_b = " << p.m_b;
	return cout;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

using namespace std;
class Person
{
public:
	Person()
	{
		m_Num = 0;
	}
	int m_Num;
};

假如我们要实现:

Person p;
p++;
++p;

我们就需要用到递增运算符重载

前置++

using namespace std;
class Person
{
public:
	Person()
	{
		m_Num = 0;
	}
	//重载前置++运算符  返回引用为了一直对一个数据进行递增操作
	Person& operator++()
	{
		m_Num++;
		return *this;
	}
	int m_Num;
};

后置++:

class Person
{
	friend ostream& operator<<(ostream& cout, Person p);
public:
	Person()
	{
		m_Num = 0;
	}
	//重载后置++运算符   用占位参数来区分后置++
	Person operator++(int)
	{
		Person  temp = *this;
		m_Num++;
		return temp;
	}
	int m_Num;
};

赋值运算符重载

C++编译器会有默认的赋值运算符重载,
赋值运算符operator=,对属性进行值拷贝
但这个默认的赋值运算符operator=,只能对属性进行值拷贝也就是浅拷贝,
如果要进行深拷贝的话,就需要自己写这个赋值运算符重载

#include<iostream>
using namespace std;
class Person
{
public:
	Person(int a)
	{
		age = new int(a);
	}
	Person& operator=(Person& p)  //赋值运算符重载
	{
		if (age != NULL)
		{
			delete age;
			age = NULL;
		}
		age = new int(*p.age);
		return *this;  //返回这个时赋值时可以进行连等操作
	}
	~Person()    //析构函数,在程序结束后释放空间
	{
		if (age != NULL)
		{
			delete age;
			age = NULL;
		}
	}
public:
	int* age;

};

关系运算符重载

作用:重载关系运算符,可以让两个自定义类型的对象进行对比操作
比如:
比较两个类的大小关系
要实现这个就需要重载关系运算符

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
	bool operator==(Person& p)
	{
		if (this->age == p.age && this->name == p.name)
			return true;
		else
			return false;
	}
	bool operator!=(Person& p)
	{
		if (this->age != p.age || this->name == p.name)
			return true;
		else
			return false;
	}
public:
	string name;
	int age;
};

这样我们就可以进行下面操作了

int main()
{
	Person p1;
	p1.age = 18;
	p1.name = "abd";
	Person p2;
	p2.age = 18;
	p2.name = "abd";
	if (p1 == p2)
		cout << "yes" << endl;
	if (p1 != p2)
		cout << "yes" << endl;
}

函数调用运算符重载

函数调用运算符()也可以进行重载
由于重载后的使用方法非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活。

如果我们要写一个打印类

#include<iostream>
#include<string>
class Mprint
{
public:
	void operator()(string s)
	{
		cout << s << endl;
	}
};
int main()
{
	Mprint mprintf;
	mprintf("hello word");   //由于使用方法特别像函数调用,所以被称为仿函数
}

如果想实现一个加法类:

#include<iostream>
#include<string>
class MyAdd
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};
int main()
{
	MyAdd add;
	int ret = add(20, 30);
	cout << ret << endl;
	//匿名对象调用
	cout << MyAdd()(20, 30) << endl;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

binary~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值