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();
}
相关推荐
智者知已应修善业31 分钟前
【删除有序数组中的重复项 II之O(N)算法】2024-1-31
c语言·c++·经验分享·笔记·算法
爱装代码的小瓶子33 分钟前
【c++进阶】C++11新特性:一切皆可{}初始化
开发语言·c++·visual studio
xiaoye-duck40 分钟前
吃透C++类和对象(中):构造函数与析构函数深度解析
c++
AA陈超40 分钟前
Lyra Starter Game 中 GameFeature 类(如 ShooterCore)的加载流程
c++·笔记·学习·ue5·虚幻引擎
加成BUFF1 小时前
C++入门讲解3:数组与指针全面详解
开发语言·c++·算法·指针·数组
老王熬夜敲代码1 小时前
linux系统IO
linux·笔记
stars-he1 小时前
FPGA学习笔记(6)逻辑设计小结与以太网发送前置
笔记·学习·fpga开发
天若有情6731 小时前
我发明的PROTO_V4协议:一个让数据“穿上迷彩服”的发明(整数传输协议)
网络·c++·后端·安全·密码学·密码·数据
加油=^_^=1 小时前
【C++11】特殊类设计 | 类型转换
c++·单例模式·类型转换
锦瑟弦音2 小时前
跑酷游戏开发笔记3 && 游戏开始场景 cocos 3.8.7
javascript·笔记·游戏