c++之运算符重载

对已有的运算符重新定义,以适应不同的数据类型。

  • 加号运算符重载+

1、通过成员函数重载

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
	//通过成员函数重载加号运算符
	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;
	}
	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);

 3、运算符重载操作也可以发生函数重载(函数参数不同,参数类型不同,其他都相同)

Person operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}

调用

Person p4=p3+10;
//本质为:Person p4=operator+(p3,10);
  • 左移运算符重载<<

通常不用成员函数重载,只能利用全局函数重载。

//ostream 输入流
ostream& operator<<(ostream& cout, Person& p)
{
	cout << "m_A=" << p.m_A << "m_B=" << p.m_B;
	return cout;
}
  • 递增运算符重载++

1、前置递增:类内定义函数

​
class MyInteger
{
public:
	MyInteger& operator++()
	{
		//先++
		m_Num++;
		//再返回自身
		return *this;
	}
	int m_Num;
	
};

​

后置递增 ,用int做占位参数,用于表示后置++,不用传引用值&。

class MyInteger
{
public:
	
	//后置++
	MyInteger operator++(int)//int代表占位参数,用于表示后置++,不传引用值
	{
		//先记录自身结果
		MyInteger temp = *this;
		//递增操作
		m_Num++;
		//将记录结果做返回
		return temp;
	}

	int m_Num;
	
};
  • 赋值运算符重载=

编译器会默认给每个类提供4个函数:

1、默认构造函数(无参,函数体为空)

2、默认析构函数(无参,函数体为空)

3、默认拷贝构造函数,对属性进行值拷贝(浅拷贝)

4、赋值运算符operator=,对属性进行值拷贝(浅拷贝)

浅拷贝会存在对一块堆区重复释放的问题,用深拷贝解决。如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝的问题。

内置赋值运算:a=b=c;     重载赋值运算符:P1=P2=P3;

class Person
{
public:
	//赋值运算符重载
	Person& operator=(Person& p)
	{
		//m_Age = p.m_Age;编译器提供的浅拷贝操作,会出错
		if(m_Age!=NULL)//先判断是否有属性在堆区,如果有,就先释放再进行深拷贝
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);
		return *this;
	}
	
	int* m_Age;
};
  • 关系运算符重载==、!=

class Accustom
{
public:
	Accustom(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	//类内重载
	bool operator==(Accustom& a)
	{
		if (this->m_Name == a.m_Name && this->m_Age == a.m_Age)
		{
			return true;
		}
		return false;
	}
	string m_Name;
	int m_Age;
};
void test01()
{
	Accustom a1("王倩", 21);
	Accustom a2("王倩", 21);
	if (a1 == a2)
	{
		cout << "a1和a2是相等的" << endl;
	}

}
int main()
{
	test01();
	return 0;
}
  • 函数调用运算符重载()----仿函数

打印函数重载

class Ankle
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};
void test02()
{
	Ankle an;
	an("Hello World");
}

int main()
{
	test02();
	return 0;
}

 加法函数

class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};
void test03()
{
	MyAdd add;
	int ret = add(10, 40);
	cout << ret << endl;
	//匿名对象调用
	cout << "MyAdd()(100,400)=" << MyAdd()(100, 400) << endl;
}

int main()
{
	test03();
	return 0;
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值