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();
}
相关推荐
青山是哪个青山15 分钟前
LangChain 学习笔记(三):LangSmith 调试与监控
笔记·学习·langchain
C++ 老炮儿的技术栈1 小时前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐
蒸蒸yyyyzwd2 小时前
cpp选手备战后端学习笔记day6 秋招笔试题
笔记·学习
qz_Serene3 小时前
C++:类和对象(上)
开发语言·c++
DeviceHub4 小时前
硬件工程师选型笔记:ALPS SKPMAPE010 与 Tonevee TS-3025 贴片轻触开关 PIN TO PIN 评估指南
笔记
郝学胜-神的一滴5 小时前
干货版《算法导论》17:二叉树核心原理、遍历逻辑与高阶实操全解
数据结构·c++·python·算法·计算机·编程
hetao17338375 小时前
2026-08-09~08-14 hetao1733837 的刷题记录
c++·算法
疯狂打码的少年5 小时前
【数据结构】二叉树的性质(五大性质+计算)
数据结构·笔记·算法
Capricorn19886 小时前
知芽研究问题看板无法推进?从孵化区到笔记的机制排查指南
大数据·论文阅读·人工智能·笔记·学习方法·论文笔记
闪闪发亮的小星星7 小时前
相机脱靶量VS精跟踪精度
笔记