【C++学习手札】一文带你初识运算符重载

                                         食用指南:本文在有C基础的情况下食用更佳   

                                        🍀本文前置知识: C++类 

                                        ♈️今日夜电波:クリームソーダとシャンデリア—Edo_Ame江户糖

                                                                1:20 ━━━━━━️💟──────── 3:40
                                                                    🔄   ◀️   ⏸   ▶️    ☰ 

                                      💗关注👍点赞🙌收藏您的每一次鼓励都是对我莫大的支持😍 


目录

💓一、运算符重载基本概念

什么是运算符重载?

运算符重载简要干货

可重载的运算符有哪些? 

💗二、前置知识-友元函数 

 什么是友元函数?

 友元函数的语法

 💞三、运算符重载

运算符重载的语法

一步一步带你实现运算符重载(以<<为例)

运算符重载作为成员函数以及全局函数的实现(以+为例)

全局函数

成员函数

💕四、++和--运算符重载 (重要、常用)

💘具体实现用例 


一、运算符重载基本概念

什么是运算符重载?

        运算符重载, 就是对已有的运算符重新进行定义, 赋予其另一种功能, 以适应不同
的数据类型。
        运算符重载(operator overloading)只是一种”语法上的方便”,也就是它只是另一种函
数调用的方式。

        在 c++中, 可以定义一个处理类的新运算符。 这种定义很像一个普通的函数定义,只是函数的名字由关键字 operator 及其紧跟的运算符组成。 差别仅此而已。 它像任何其他函数一样也是一个函数, 当编译器遇到适当的模式时, 就会调用这个函数。

运算符重载简要干货

        运算符重载的目的:简化操作 让已有的运算符 适应适应不同的数据类型。
        语法:函数的名字由关键字operator及其紧跟的运算符组成
        比如:重载+运算符 ==>     operator+ 重载=号运算     ==>     operator=
        注意:重载运算符 不要更改 运算符的本质操作(+是数据的相加 不要重载成相减)

         栗子:(以下为重载了<<运算符的类)

class Data
{
	friend ostream& operator<<(ostream& out, Data& ob);//友元函数,经常与运算符重载搭配使用
private:
	int a;
	int b;
public:
	Data()
	{
		cout << "无参的构造函数" << endl;
		a = 0;
		b = 0;
	}
	Data(int a, int b) :a(a), b(b)
	{
		cout << "有参构造" << endl;
		//this‐>a = a;
			//this‐>b = b;
	}
	void showData(void)
	{
		cout << "a = " << a << ", b= " << b << endl;
	}
	~Data()
	{
		cout << "析构函数函数" << endl;
	}
};

ostream& operator<<(ostream& out, Data& ob)
 {
	 out << "a = " << ob.a << ", b = " << ob.b;
	 return out;
 }

        解释:

        为了简化类中访问私有数据较为困难的问题,运用友元函数(下小点会提到)同重载运算符的结合,得以运用我们较为常用的<<直接输出数据。

可重载的运算符有哪些? 

        几乎 C 中所有的运算符都可以重载, 但运算符重载的使用时相当受限制的。 特别是不能使用 C 中当前没有意义的运算符(例如用**求幂)不能改变运算符优先级, 不能改变运算符的参数个数。 这样的限制有意义, 否则, 所有这些行为产生的运算符只会混淆而不是澄清寓语意。

        一张图囊括~ 


二、前置知识-友元函数 

 什么是友元函数?

一句话概括:C++允许 友元 访问 私有数据。

 友元函数的语法

friend+定义的函数

      注意:  friend关键字只出现在声明处 其他类、类成员函数、全局函数都可声明为友元 友元函数不是类的成员,不带this指针 友元函数可访问对象任意成员属性,包括私有属性。

        栗子: (创建一个房间类,你只准许你的朋友进入你的卧室,但是客厅是谁都可以进的)

class Room
	 {
	 //将goodGayVisit作为类的友元函数
		 //goodGayVisit 访问 类中所有数据 但是 它不是类的成员
		 friend void goodGayVisit(Room & room);
 private:
	 string bedRoom;//卧室
 public:
	 string sittingRoom;//客厅
 public: 
	 Room()
	 {
	 this-> bedRoom = "卧室";
	 this-> sittingRoom = "客厅";
	 }
	};

 // 普通全局函数 作为 类的友元
 //好基友 访问 我的房间
 void goodGayVisit(Room & room)
 {
	 cout << "好基友访问了你的" << room.sittingRoom << endl;
	 cout << "好基友访问了你的" << room.bedRoom << endl;//ok
	 }
 void test01()
 {
	 Room myRoom;
	 goodGayVisit(myRoom);
}

        friend在这里可以访问对象任意成员属性,包括私有属性。因此,本来不能访问的私有数据,在friend的情况下就可以访问了!结果如下:

        此为普通全局函数 作为 类的友元 。当然,也有类的某个成员函数 作为 另一个类的友元;一个类整体 作为 另一个类的友元等等。

        而我们的友元函数大多应用在重载运算符上!

         本文仅仅对友元函数做简单介绍,如果大家需要详解,请在评论区或者私信踢我一脚o(╯□╰)o,作者肯定会出一篇的!


 三、运算符重载

运算符重载的语法

(根据自身改变的返回类型)operator + 重载的运算符(根据实际情况改变的传参)
  1. 函数声明:运算符重载是通过在类中定义特殊的成员函数来实现的。这些成员函数被称为运算符重载函数。例如,如果要重载"+"运算符,则需要在类中声明一个名为"operator+"的函数。

  2. 函数名:运算符重载函数的命名规则是以"operator"关键字开始,后面跟着要重载的运算符符号。例如,要重载"+“运算符,函数名应为"operator+”。

  3. 参数列表:运算符重载函数的参数列表取决于所重载的运算符。例如,对于二元运算符如"+", “-”, “*”, “/“等,参数列表应包含一个额外的参数,表示右操作数。对于一元运算符如”++”, "– – "等,参数列表不需要额外的参数。

  4. 返回类型:运算符重载函数的返回类型取决于所重载的运算符。例如,对于"+"运算符,返回类型通常是所操作对象的类型。

  5. 成员函数或友元函数:运算符重载函数可以作为类的成员函数或友元函数来定义。成员函数形式的运算符重载函数将使用对象本身作为左操作数,而友元函数形式的运算符重载函数将不使用任何对象。


一步一步带你实现运算符重载(以<<为例)

        注意:此代码未能实现重载 下文为对用cout来输出类的一个引入

#define _CRT_SECURE_NO_WARNINGS 01
#include <iostream>
#include<string.h>
 using namespace std;
 class Person
 {
 private:
	 char* name;
	 int num;
 public:
	 Person(char* name, int num)
		 {
		 this-> name = new char[strlen(name) + 1];
		 strcpy(this-> name, name);
		 this-> num = num;
		 cout << "有参构造" << endl;
		 }
	 //普通的成员函数
		 void printPerson(void)
		 {
		 cout << "name = " << name << ", num = " << num << endl;
		 }
	 ~Person()
		 {
		 if (this-> name != NULL)
			 {
			 delete[] this-> name;
			 this-> name = NULL;
			 }
		 cout << "析构函数" << endl;
		 }
	 };
 int main(int argc, char* argv[])
 {
	 char arr[] = "lucy";
	 Person ob1(arr, 18);
	 //普通的成员函数 遍历信息
	 //ob1.printPerson();
		
	 //cout默认输出方式 无法识别 自定义对象 输出格式
	cout<<ob1<<endl;//err
	return 0;
 }

        运行改代码,我们发现编译器报错!如下图:

         这个时候我们就需要对运算符进行重载了!

        那么问题又来了?如何重载运算符呢?根据上文所提到的语法,我们做出以下的操作:

        运用operator来重载<<运算符

 ostream& operator<<(ostream& out, Person& ob)//out=cout, ob =ob1
	  {
	  //重新实现 输出格式
		  out << ob.name << ", " << ob.num;
		  //每次执行为 返回值得到cout
		  return out;
	  }

        注意:ostream为cout的类型,定义ostream&为返回类型是为了作为起到链接的效果,如:

cout<<ob1<<ob2<<endl;ostream&返回out,然后再次被后面所调用,一直反复调用下去。

        然而,进行了运算符重载,就能实现我们想要的效果了吗?答案是不能,见下图:

         造成这样的原因是什么呢?还是类的封装问题,私有的数据不能被外界所访问!这时,我们就需要用到友元函数来帮助我们实现了!

         于是,我们将operator<<设置成友元:

#define _CRT_SECURE_NO_WARNINGS 01
#include <iostream>
#include<string.h>
 using namespace std;
 class Person
 {
	 //设置成友元函数 在函数内 访问Person类中的所有数据
	  friend ostream & operator<<(ostream & out, Person & ob);
 private:
	 char* name;
	 int num;
 public:
	 Person(char* name, int num)
		 {
		 this-> name = new char[strlen(name) + 1];
		 strcpy(this-> name, name);
		 this-> num = num;
		 cout << "有参构造" << endl;
		 }
	 //普通的成员函数
		 void printPerson(void)
		 {
		 cout << "name = " << name << ", num = " << num << endl;
		 }
	 ~Person()
		 {
		 if (this-> name != NULL)
			 {
			 delete[] this-> name;
			 this-> name = NULL;
			 }
		 cout << "析构函数" << endl;
		 }
	 };
 ostream& operator<<(ostream& out, Person& ob)//out=cout, ob =ob1
	  {
	  //重新实现 输出格式
		  out << ob.name << ", " << ob.num;
		  //每次执行为 返回值得到cout
		  return out;
	  }
 int main(int argc, char* argv[])
 {
	 char arr[] = "lucy";
	 Person ob1(arr, 18);
	 //普通的成员函数 遍历信息
	 //ob1.printPerson();
		
	 //cout默认输出方式 无法识别 自定义对象 输出格式
	cout<<ob1<<endl;//err
	return 0;
 }

        实现效果如下:


运算符重载作为成员函数以及全局函数的实现(以+为例)

全局函数

        这里同上面的栗子大致一样,不过多叙述

#include <iostream>
 #include<string.h>
 using namespace std;
 class Person
 {
	 //设置成友元函数 在函数内 访问Person类中的所有数据
	 friend ostream & operator<<(ostream & out, Person & ob);
	 friend Person operator+(Person & ob1, Person & ob2);
 private:
	 char* name;
	 int num;
 public:
	 Person(){
		 this-> name = NULL;
		 this-> num = 0;
		 cout << "无参构造" << endl;
		 }
	 Person(char* name, int num)
		 {
		 this-> name = new char[strlen(name) + 1];
		 strcpy(this-> name, name);
		 this-> num = num;
		 cout << "有参构造" << endl;
		 }
	 //普通的成员函数
		 void printPerson(void)
		 {
		 cout << "name = " << name << ", num = " << num << endl;
		 }
	 ~Person()
		 {
		 if (this-> name != NULL)
			 {
			 delete[] this-> name;
			 this-> name = NULL;
			 }
		 cout << "析构函数" << endl;
		 }
	 };

 //全局函数作为友元 完成运算符重载<<
 ostream & operator<<(ostream & out, Person & ob)//out=cout, ob =ob1
 {
	 //重新实现 输出格式
		 out << ob.name << ", " << ob.num;
	
		 //每次执行为 返回值得到cout
		 return out;
	 }
 //全局函数作为友元 完成运算符重载+
 Person operator+(Person & ob1, Person & ob2)//ob1 ob2
 { //name+name(字符串追加)
 char* tmp_name = new char[strlen(ob1.name) + strlen(ob2.name) + 1];
 strcpy(tmp_name, ob1.name);
 strcat(tmp_name, ob2.name);

 //num+num(数值相加)
 int tmp_num = ob1.num + ob2.num;
 Person tmp(tmp_name, tmp_num);

 //释放tmp_name的空间
 if (tmp_name != NULL)
 {
	 delete[] tmp_name;
	 tmp_name = NULL;
	 }

 return tmp;
 }


 void test02()
 {
	 char arr[] = "lucy";
	 Person ob1(arr, 18);  
	 char arr2[] = "bob";
	 Person ob2(arr2, 19);
	
		 cout << ob1 << endl;
		cout << ob2 << endl;
	 //Person ob3 = operator+(ob1,ob2);
		 Person ob3 = ob1 + ob2;
	 cout << ob3 << endl;
	 }
 int main(int argc, char* argv[])
 {
	 test02();
	 return 0;
 }
成员函数
#include <iostream>
 #include<string.h>
 using namespace std;
 class Person
 { //设置成友元函数 在函数内 访问Person类中的所有数据
 friend ostream & operator<<(ostream & out, Person & ob);
 private:
	 char* name;
	 int num;
 public:
	 Person()
		 {
		 this-> name = NULL;
		 this-> num = 0;
		 cout << "无参构造" << endl;
		 }
	 Person(char* name, int num)
		 {
		 this-> name = new char[strlen(name) + 1];
		 strcpy(this-> name, name);
		 this-> num = num;
		 cout << "有参构造" << endl;
		 }
	 //成员函数 完成运算符重载 ob1用this代替 ob2用参数ob代替
		 Person operator+(Person & ob)
		 {
		 //this ==> &ob1
			
			 //name+name(字符串追加)
		 char* tmp_name = new char[strlen(this-> name) + strlen(ob.name) + 1];
		 strcpy(tmp_name, this-> name);
		 strcat(tmp_name, ob.name);
		
			 //num+num(数值相加)
		 int tmp_num = this-> num + ob.num;
		 Person tmp(tmp_name, tmp_num);
		
			 //释放tmp_name的空间
			 if (tmp_name != NULL)
			 {
			 delete[] tmp_name;
			 tmp_name = NULL;
			 }
		 return tmp;
		 }
	
		 //普通的成员函数
		 void printPerson(void)
		 {
		 cout << "name = " << name << ", num = " << num << endl;
		 }
	 ~Person()
		 {
		 if (this-> name != NULL)
			 {
			 delete[] this-> name;
			 this-> name = NULL;
			 }
		 cout << "析构函数" << endl;
		 }
	 };

 //全局函数作为友元 完成运算符重载<<
 ostream & operator<<(ostream & out, Person & ob)//out=cout, ob =ob1
 {
	 //重新实现 输出格式
		 out << ob.name << ", " << ob.num;
	
		 //每次执行为 返回值得到cout
		 return out;
	 }

 void test03()
 {
	 char arr[] = "lucy";
	 char arr2[] = "bob";
	 Person ob1(arr, 18);
	 Person ob2(arr2, 19);
	
		 //Person ob3 = ob1.operator+(ob2);
		 Person ob3 = ob1 + ob2;
	 cout << ob3 << endl;
	 }

 int main(int argc, char* argv[])
 { test03();

 return 0;
 }

        在运算符重载运算符时,如果我们以成员函数的方式定义,则可以直接访问类中的数据,无需再使用友元函数来定义。因此我们在重载运算符时最好是以成员函数的方式重载!


四、++和--运算符重载 (重要、常用)

        不知道大家有没有一个疑惑如果我们实现前置+ +、后置+ +以及前置- -、后置--,运用operator时如何区分他们呢?

        此时,我们又要提到一个概念,当编译器看到++a(前置++),它就调用operator++(a),当编译器看到a++(后置++),它就会去调用operator++(a,int)。 - -也是同样的道理,具体实现如下:

具体实现用例 

#include <iostream>

using namespace std;
 class Data
 {
	 friend ostream & operator<<(ostream & out, Data & ob);
 private:
	 int a;
	 int b;
 public:
	Data()
		 {
		 cout << "无参的构造函数" << endl;
		 a = 0;
		 b = 0;
		 }
	 Data(int a, int b) :a(a), b(b)
		 {
		 cout << "有参构造" << endl;
		 //this‐>a = a;
			 //this‐>b = b;
			 }
	 void showData(void)
		 {
		 cout << "a = " << a << ", b= " << b << endl;
		 }
	 ~Data()
		 {
		 cout << "析构函数函数" << endl;
		 }
	
		 //成员函数 重载前置++ ++ob1 (先加 后使用)
		 //编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator++()
		 Data & operator++()//++ob1
		 { //先加
		 a++;//this‐>a = this‐>a +1
	 b++;//this‐>b = this‐>b +1
	 //后使用
		 return *this;
	 }
	 //成员函数 重载后置++ ob1++ (先使用 后加)
		 //编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator ++(int)
		 Data & operator++(int)//ob1++
		 {
		 //先使用(备份加之前的值)
			 static Data old = *this;
		
			 //后加
			a++;
			b++;
		
			 //返回备份值
			 return old;
		 }
	
		 //重载前置‐‐ ‐‐ob3
		 //编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator‐‐()
		 Data & operator--()
		 {
		 //先减
			a--;
			b--;
		
			 //后使用(返回)
			 return *this;
		 }
	
		 //重载后‐‐ ob4‐‐
		 //编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator++(int)
		 Data & operator--(int)
		 {
		 //先使用
			 static Data old = *this;
			 //再减
			a--;
			b--;
		
			 return old;
		 }
	
		
		 };
 //普通全局函数 作为类的友元 重载<<运算符
 ostream & operator<<(ostream & out, Data & ob)
 {
	 out << "a = " << ob.a << ", b = " << ob.b;
	 return out;
	 }
 void test01()
 {
	 Data ob1(10, 20);
	 ob1.showData();
	
		 //重载<<直接输出自定义对象的值
		 //operator<<(cout,ob1);
		 cout << ob1 << endl;
	
		 //成员函数 重载 ++运算符
		 cout << ++ob1 << endl;
	
		 Data ob2(10, 20);
	 cout << ob2++ << endl;
	 cout << ob2 << endl;
	
		 //成员函数 重载 ‐‐运算符
		 Data ob3(10, 20);
	 cout << "ob3 " << ob3 << endl;
	 cout << --ob3 << endl;
	
		 Data ob4(10, 20);
	 cout << "ob4 " << ob4 << endl;
	 cout << ob4-- << endl;
	 cout << "ob4 " << ob4 << endl; 
		
		 }
 int main(int argc, char* argv[])
 {
	 test01();
	 return 0;
 }

        效果如下: 


                感谢你耐心的看到这里ღ( ´・ᴗ・` )比心,如有哪里有错误请踢一脚作者o(╥﹏╥)o!  

                                 

                                                                 给个三连再走嘛~      

  • 53
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 79
    评论
好的,你的问题是关于Python函数第二次运行报错和retry的简单用法。 首先,如果一个Python函数在第二次运行时出现了错误,可能是由于函数内部的变量状态发生了变化,导致程序出错。为了解决这个问题,可以使用retry功能来多次尝试运行该函数,直到它成功为止。 使用retry功能可以在函数出现错误时自动重试,可以避免动重复运行该函数,从而提高效率。 下面是一个简单的示例代码,演示了如何使用retry功能: ```python import time from retry import retry @retry(delay=1, backoff=2, max_delay=4) def my_func(): print("Running my_func...") # 模拟函数出错 if time.time() % 2 == 0: raise ValueError("Oops! Something went wrong...") else: print("Success!") my_func() ``` 在这个示例代码中,我们使用了retry库中的retry装饰器来装饰my_func函数,该函数会运行多次,直到成功为止。在retry装饰器中,我们设置了最大延迟时间为4秒,每次重试前等待1秒,重试间隔时间呈指数级增长,即第一次重试后等待1秒,第二次重试后等待2秒,第三次重试后等待4秒,以此类推,直到达到最大延迟时间为止。 当my_func函数第一次运行时,它会输出“Running my_func...”和“Success!”,并正常结束。当函数第二次运行时,它会抛出一个ValueError异常,并重试多次,直到成功为止。 希望这个简单的示例可以帮助你理解如何使用retry功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 79
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕斯( ˘▽˘)っ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值