黑马程序员C++类和对象【5】 —— 运算符重载(蓝桥杯必备知识)万字超详解
创始人
2024-04-03 01:18:48
0

目录

🤡加号运算符重载

🤡左移运算符重载

🤡递增运算符重载

🤡递减运算符重载

🤡赋值运算符重载

🤡关系运算符重载

🤡函数调用运算符重载(仿函数)


🤡加号运算符重载

如果现在我们希望对对象的属性进行求和,我们可能会想到用+直接进行相加,那么就让我们先来试试吧,下面是我实例化两个对象的两个属性相加,结果编译器会报错,说我的操作数和运算符不匹配,那么这时候就需要自己手动重载运算符了。

作用:实现两个自定义数据类型相加的运算

使用运算符重载(注意:重载函数名必须是编译器提供的operator+)


成员函数重载

//运算符重载
class Person {
public://1、成员函数重载+号Person operator+(Person& p){Person temp;//this指针指向的是调用该成员函数的对象(p1),这里面的p是指传进来的p2的引用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;};
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;    //等价于 p1.operator+(p2)cout << p3.m_A << " " << p3.m_B << endl;
}int main() {test01();    //调用system("pause");return 0;
}

全局函数重载

//运算符重载
class Person {
public://1、成员函数重载+号//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;};//2、全局函数重载  + 号
Person operator+(Person &p1,Person &p2)
{Person temp;temp.m_A = p1.m_A + p2.m_B;temp.m_B = p1.m_B + p2.m_B;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;		//成员函数重载本质调用等价于 p1.operator+(p2)Person p3 = p1 + p2;			//全局函数重载本质调用等价于 operator+(p1,p2)cout << p3.m_A << " " << p3.m_B << endl;
}int main() {test01();system("pause");return 0;
}

运算符重载也可以发生函数重载

class Person {
public:Person() {};Person(int a, int b){this->m_A = a;this->m_B = b;}//成员函数实现 + 号运算符重载Person operator+(const Person& p) {Person temp;temp.m_A = this->m_A + p.m_A;temp.m_B = this->m_B + p.m_B;return temp;}public:int m_A;int m_B;
};//全局函数实现 + 号运算符重载
//Person operator+(const Person& p1, const Person& p2) {
//	Person temp(0, 0);
//	temp.m_A = p1.m_A + p2.m_A;
//	temp.m_B = p1.m_B + p2.m_B;
//	return temp;
//}//运算符重载 可以发生函数重载 
Person operator+(const Person& p2, int val)
{Person temp;temp.m_A = p2.m_A + val;temp.m_B = p2.m_B + val;return temp;
}void test() {Person p1(10, 10);Person p2(20, 20);//成员函数方式Person p3 = p2 + p1;  //相当于 p2.operaor+(p1)cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;Person p4 = p3 + 10; //相当于 operator+(p3,10)cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;}int main() {test();system("pause");return 0;
}

 总结1:对于内置的数据类型的表达式的的运算符是不可能改变的;

总结2:不要滥用运算符重载


🤡左移运算符重载

比如这个例子,显然下面这个不能输出的。

左移运算符重载的作用:可以输出自定义数据类型

可以转到定义查看cout是ostream类型的 

//左移运算符重载class Person {friend ostream& operator<<(ostream& out, Person& p);public:Person(int a, int b){this->m_A = a;this->m_B = b;}//成员函数 实现不了(  p << cout 不是我们想要的效果 )//void operator<<(Person& p){//}private:int m_A;int m_B;
};//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {//内部就是一个简单的对象属性的输出out << "a:" << p.m_A << " b:" << p.m_B << endl;;return out;
}void test() {Person p1(10, 20);//如果返回的是void型,后面就不能追加输出了cout << p1 << "hello world" << endl; //链式编程
}int main() {test();system("pause");return 0;
}

 总结:重载左移运算符配合友元可以实现输出自定义数据类型


🤡递增运算符重载

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


class MyInteger {friend ostream& operator<<(ostream& out, MyInteger myint);public:MyInteger() {m_Num = 0;}//前置++MyInteger& operator++() {//先++m_Num++;//再返回return *this;}//后置++MyInteger operator++(int) {//先返回MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;m_Num++;return temp;}private:int m_Num;
};ostream& operator<<(ostream& out, MyInteger myint) {out << myint.m_Num;return out;
}//前置++ 先++ 再返回
void test01() {MyInteger myInt;cout << ++myInt << endl;cout << myInt << endl;
}//后置++ 先返回 再++
void test02() {MyInteger myInt;cout << myInt++ << endl;cout << myInt << endl;
}int main() {test01();//test02();system("pause");return 0;
}

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


🤡递减运算符重载

class MyInteger {//友元函数,是函数可以调用类里面的私有属性friend ostream& operator<<(ostream& out, MyInteger myint);
public:MyInteger() {//给m_Num赋初值。m_Num = 2;}//前置--MyInteger& operator--() {//先 -- --m_Num;//再返回return *this;}//后置--MyInteger operator--(int) {//先返回MyInteger temp = *this; //记录当前本身的值,然后让本身的减1,但是返回的是以前的值,达到先返回后--;m_Num--;return temp;}
private:int m_Num;
};//全局函数重载左移运算符用于输出
ostream& operator<<(ostream& out, MyInteger myint) {out << myint.m_Num;return out;
}//前置-- 先-- 再返回
void test01() {MyInteger myInt;cout <<--myInt << endl;cout << myInt << endl;
}//后置-- 先返回 再--
void test02() {MyInteger myInt;cout << myInt-- << endl;cout << myInt << endl;
}int main() {test01();test02();system("pause");return 0;  
}

 递减和递增一样,前置返回引用,后置返回值。


🤡赋值运算符重载

 

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

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

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

  3. 默认拷贝构造函数,对属性进行值拷贝

  4. 赋值运算符 operator=, 对属性进行值拷贝

 

class Person
{
public:Person(int age) {m_Age = new int(age);}~Person() {if (m_Age != NULL) {delete m_Age;m_Age = NULL;}}int *m_Age;
};void test01()
{Person p1(18);cout << "p1的年龄为:" << *p1.m_Age << endl;Person p2(28);p2 = p1;cout << "p1的年龄为:" << *p2.m_Age << endl;
}int main() {test01();system("pause");return 0;
}

写了个析构函数,释放了自己手动在堆上面申请的内存 ,结果出现崩溃了。。。

 这里实际上是浅拷贝的问题。

 p1和p2公用一块堆内存18,并没有再重新开辟和p1的属性值一样的空间给p2,他们都指向这一块内存。

 在函数执行完之后,p2被释放了,再判断p2空间是否为空,不为空释放一次,p1进行同样操作。

堆区内存重复释放,重新崩溃!

解决方案:利用深拷贝,解决浅拷贝带来的问题。

 

	//重载赋值运算符void 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);

 直接在类里面添加上面代码就可以实现了

但是,你以为这就完了吗

如果这时候要求连续赋值操作,p3 = p2 = p1;则会报错,为啥呢?

仔细看,你前面返回的是void类型,这里需要返回对象本体Person&。

重载部分代码:

	//重载赋值运算符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;}

 总结:

        先在堆区开辟内存

重载流程:

  1.         判断是否有内存,进行清空操作。
  2.         进行深拷贝
  3.         返回自身

🤡关系运算符重载

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

class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;};//==的重载bool operator==(Person & p){if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}//!=的重载bool operator!=(Person & p){if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return false;}else{return true;}}string m_Name;int m_Age;
};void test01()
{//int a = 0;//int b = 0;Person a("孙悟空", 18);Person b("孙悟空", 18);if (a == b){cout << "a和b相等" << endl;}else{cout << "a和b不相等" << endl;}if (a != b){cout << "a和b不相等" << endl;}else{cout << "a和b相等" << endl;}
}int main() {test01();system("pause");return 0;
}

🤡函数调用运算符重载(仿函数)

  • 函数调用运算符 () 也可以重载

  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数

  • 仿函数没有固定写法,非常灵活

class MyPrint
{
public:void operator()(string text){cout << text << endl;}};
void test01()
{//重载的()操作符 因为调用的时候和函数调用很像,所以也称为仿函数MyPrint myFunc;myFunc("hello world");
}class MyAdd
{
public:int operator()(int v1, int v2){return v1 + v2;}
};void test02()
{MyAdd add;int ret = add(10, 10);cout << "ret = " << ret << endl;//匿名函数对象调用  cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}int main() {test01();test02();system("pause");return 0;
}

记住:仿函数非常灵活,没有固定的写法。

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...