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();
}
相关推荐
yi0116 分钟前
LeetCode 424. 替换后的最长重复字符|不定长滑动窗口:如何判断窗口是否合法?
笔记·python·算法·leetcode·双指针·滑动窗口
jimy115 分钟前
C++ 类成员函数的内联定义(inline)详解
开发语言·c++
杨逢昌工厂6S管理31 分钟前
116——杨逢昌:归位三定闭环法的旺季轻量化执行方案与工具清单
经验分享·笔记·职场和发展·学习方法
想喝奶茶啾啾32 分钟前
AMBA CHI架构以及规范中使用的术语 翻译笔记(1)
笔记·arm·chi
sanqima35 分钟前
CreateThread、_beginthreadex、AfxBeginThread三种线程创建的区别与联系
c++·tls·线程创建
sanqima41 分钟前
依赖TLS和模块状态的MFC类
c++·mfc·tls·module state
All for pursuit.1 小时前
【动态规划-11】279.完全平方数
数据结构·c++·算法·leetcode·动态规划
瑶山1 小时前
Windows + WSL2 Ubuntu + VS Code配置C++开发环境
开发语言·c++·windows·vscode·wsl
小米里的大麦2 小时前
19 C、C++ 操作 MySQL
c语言·c++·mysql
有毒的教程2 小时前
Dev-C++ 使用扩展库两种方式:DevPak一键安装 / 手动配置(重点)
android·c++·adb