C++笔记-21-运算符重载

运算符重载

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

加号运算符重载

cpp 复制代码
#include<iostream>
using namespace std;
class Person
{
public:
	int m_A;
	int m_B;
	//本质是p3=p1.operator+(p2);
	//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;
	//}
};
//本质是p3=operator+(p1,p2);
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 operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
void test01()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3 = p1 + p2;
	cout << p3.m_A << endl;
	cout << p3.m_B << endl;

}
int main()
{
	test01();
}

总结:

  • 运算符重载也可以发生函数重,例如p3=p1+10;
  • 对于内置的数据类型的表达式的的运算符是不可能改变的
  • 不要滥用运算符重载

左移运算符重载

例如我们想cout<<p;直接输出p这个对象就打印p的内部属性,就可以重载左移运算符

cpp 复制代码
#include<iostream>
using namespace std;
class Person
{
	friend ostream& operator<<(ostream& cout, Person& p);
private:
	int m_A = 10;
	int m_B = 10;
};
ostream& operator<<(ostream& cout, Person& p)
{
	cout << p.m_A << endl << p.m_B << endl;
	return cout;
}
void test01()
{
	Person p;
	cout << p << endl;
}
int main()
{
	test01();
}

注意:

  • 利用成员函数重载左移运算符p.operator<<(cout)简化版本p<<cout
  • 所以不会利用成员函数重载<<运算符,因为无法实现cout在左侧
  • 一般类的成员变量我们会设置为私有权限,为了保证运算符的正常使用,可以将重载后的函数作为友元。

递增运算符重载

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

cpp 复制代码
#include<iostream>
using namespace std;
class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger mi);
public:
	MyInteger()
	{
		m_Num = 0;
	}
	MyInteger& operator++() {
		m_Num++;
		return *this;
	}
	//int做占位参数,与前置递增做区分
	MyInteger operator++(int) {
		MyInteger temp = *this;
		m_Num++;
		return temp;
	}
private:
	int m_Num = 0;
};
ostream& operator<<(ostream& cout, MyInteger mi)
{
	cout << mi.m_Num;
	return cout;
}
void test01()
{
	MyInteger myint;
	cout << ++(++myint) << endl;;

}
void test02()
{
	MyInteger myint;
	cout << myint++ << endl;;
	cout << myint << endl;
}
int main()
{
	//test01();
	test02();
}

总结:前置递增返回引用,后置递增返回值

赋值运算符重载

C++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符operator=,对属性进行值拷贝
    如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题。堆区需要我们手动释放,那么拷贝之后,会在析构函数里释放两次同一片地址空间,就会报错。
cpp 复制代码
#include<iostream>
using namespace std;
class Person {
public:
	Person(int age) {
		this->age = new int(age);
	}
	int* age;
	~Person()
	{
		delete age;
		age = NULL;
	}
	Person& operator=(Person& p)
	{
		if (age != NULL)
		{
			delete age;
			age = NULL;
		}
		this->age = new int(*p.age);
		return *this;
	}
};
void test01()
{
	Person p1(18);
	cout << *p1.age << endl;
	Person p2(20);
	cout << *p2.age << endl;
	Person p3(30);
	p3 = p2 = p1;
	cout << *p1.age << endl;
	cout << *p2.age << endl;
	cout << *p3.age << endl;
}
int main()
{
	test01();
}

关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

cpp 复制代码
#include<iostream>
using namespace std;
class Person
{
public:
	Person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	bool operator==(Person& p)
	{
		if (name == p.name && age == p.age)
		{
			return true;
		}
		return false;
	}
	string name;
	int age;
};
void test()
{
	Person p1("Alice", 30);
	Person p2("Alice", 31);
	if (p1 == p2)
	{
		cout << "p1和p2相等" << endl;
	}
	else {
		cout << "p1和p2不相等" << endl;
	}
}
int main()
{
	test();
}

函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
cpp 复制代码
#include<iostream>
using namespace std;
#include<string>
class MyPrint
{
public:
	void operator()(string test)
	{
		cout << test << endl;
	}
};
class MyAdd {
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};
void Pr(string test)
{
	cout << test << endl;
}
void test1()
{
	MyPrint mp;
	mp("hello,world");//由于使用起来非常类似与函数调用,所以叫仿函数
	Pr("hello,world");
}
void test2()
{
	MyAdd ma;
	int result = ma(10, 20);
	cout << result << endl;
	//匿名函数对象
	cout << MyAdd()(100, 200) << endl;
}
//仿函数非常灵活,没有固定写法.
int main()
{
	test2();
}
相关推荐
草莓熊Lotso2 小时前
C++ 继承特殊场景解析:友元、静态成员与菱形继承的底层逻辑
服务器·开发语言·c++·人工智能·经验分享·笔记·1024程序员节
yuxb732 小时前
Zabbix企业级分布式监控系统(下)
笔记·zabbix
利刃大大2 小时前
【动态规划:01背包】01背包详解 && 模板题 && 优化
c++·算法·动态规划·力扣·背包问题
im_AMBER2 小时前
算法笔记 10
笔记·学习·算法·leetcode
9ilk3 小时前
【基于one-loop-per-thread的高并发服务器】--- 前置技术
运维·服务器·c++·笔记·后端·中间件
苏比的博客5 小时前
Windows MFC添加类,变量,类导向
c++·windows·mfc
yudiandian20145 小时前
MFC - 使用 Base64 对图片进行加密解密
c++·mfc
yudiandian20145 小时前
MFC - Picture Control 控件显示图片
c++·mfc
charlie1145141919 小时前
CSS笔记4:CSS:列表、边框、表格、背景、鼠标与常用长度单位
css·笔记·学习·css3·教程