• C++系列-递增运算符重载


    • 前置自增运算符的重载函数,函数参数是空
    • 后置自增运算符的重载函数,多一个没用的参数int
    • 前置运算符返回对象的引用
    • 后置运算符返回普通对象

    前置递增运算符重载

    • (++h1) 返回的是自增后 h1 对象,自增后的h1 对象在后续运算过程中,同样会被修改,所以前置运算符的重载函数的返回值必须是引用 &。

    后置运算符的特性

    code:
    	#include 
    	using namespace std;
    	class Horse
    	{
    		friend void test01();
    	private:
    		int m_age = 3;
    	public:
    		Horse(int age)
    		{
    			m_age = age;
    		}
    		Horse& operator++()		// 必须要返回引用,因为这样才是对原对象的不断累加,才会实现原对象值夜随之改变
    		{
    			m_age ++;
    			return *this;
    		}
    	};
    	void test01()
    	{
    		Horse h1(0);
    		cout << "age: " << h1.m_age << endl;
    		cout << "age: " << (++(++(++h1))).m_age << endl;
    		cout << "age: " << h1.m_age << endl;
    	}
    	
    	int main()
    	{
    		int a1 = 0;
    		cout << ++(++a1) << endl;
    		test01();
    		system("pause");
    		return 0;
    	}
    result:
    	2
    	age: 0
    	age: 3
    	age: 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    后置运算符重载

    • 后置运算符需要先返回值,然后再实现递增,所以要先返回,并且不能返回引用,否则返回的值就是改变后的值。
    code:
    	#include 
    	using namespace std;
    	class Horse
    	{
    		friend void test01();
    		friend void test02();
    	private:
    		int m_age = 3;
    	public:
    		Horse(int age)
    		{
    			m_age = age;
    		}
    		Horse& operator++()		// 必须要返回引用,因为这样才是对原对象的不断累加,才会实现原对象值夜随之改变
    		{
    			m_age ++;
    			return *this;
    		}
    		Horse operator++(int)		// 必须要利用占位参数来区分为后置,这里先要将当前的对象属性记录下来,最后返回,因为后置++是先返回本身值,再进行+1操作
    		{
    			Horse result = *this;
    			m_age++;
    			return result;			// 不能返回局部变量的引用,所以函数的返回值是变量本身
    		}
    	};
    	void test01()
    	{
    		Horse h1(0);
    		cout << "age: " << ++(++(++h1)).m_age << endl;		//先对操作数进行累加,再返回
    		cout << "age: " << h1.m_age << endl;
    	}
    	void test02()
    	{
    		Horse h1(0);
    		cout << "age: " << h1.m_age << endl;
    		cout << "age: " << h1++.m_age << endl;
    		cout << "age: " << h1.m_age << endl;
    	}
    	
    	int main()
    	{
    		int a1 = 0;
    		cout << ++(++a1) << endl;
    		test01();
    	
    		int b1 = 0;
    		cout << b1++ << endl;
    		cout << b1 << endl;
    		//cout << (b1++)++ << endl;		//错误语句,后置的自增是无法嵌套的
    		test02();
    		system("pause");
    		return 0;
    	}
    result:
    	2
    	age: 3
    	age: 3
    	0
    	1
    	age: 0
    	age: 0
    	age: 1
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
  • 相关阅读:
    MAC 安装maven详细教程
    使用FileReader制作一个简短图片上传
    生成学习全景:从基础理论到GANs技术实战
    使用Julia语言和R语言实现K-均值
    [AIGC] Java序列化利器 gson
    个人博客项目测试报告
    js简单知识
    Es6中的拓展运算符参数解构在实际项目当中应用
    Java队列相关面试题
    【Try Hack Me】Enumerating Active Directory
  • 原文地址:https://blog.csdn.net/weixin_48668114/article/details/132921376