C++:类和对象-运算符重载-递增运算符重载

类和对象-运算符重载-递增运算符重载
1.作用:通过重载递增运算符,实现自己的整型数据
(1).基本概念
int a=10;
cout<<++a<<endl;//输出11
cout<<a<<endl;//输出11
int b=10;
cout<<b++<<endl;//输出10
cout<<b<<endl;//输出11
(2).重载前置++运算符
 

#include<iostream>
using namespace std;
class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger(){
		m_num = 0;
	}
	//重载前置++运算符
	MyInteger& operator++() {
		m_num++;
		return *this;
	}
private:
	int m_num;
};
ostream& operator<<(ostream& cout, MyInteger myint) {
	cout << "myint=" << myint.m_num;
	return cout;
}

void test01() {
	MyInteger myint;
	cout << myint << endl;
	cout << ++myint << endl;
}
int main() {
	test01();
 }

思考:在重载前置++运算符的成员函数
将MyInteger&operator++();改为MyInter operator++();会报错吗?输出结果一致吗?
答:不会报错,运行结果也一致
那为什么返回引用不返回值呢?
首先:
int a=10;
cout<<++a<<endl;//输出1
cout<<++(++a)<<ednl;//输出2
cout<<a<<endl;//输出2
说明其一直往同一个数据上进行++操作
那么我们将返回类型改为数字后
即改为MyInter operator++();
代码如下
 

#include<iostream>
using namespace std;
class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger(){
		m_num = 0;
	}
	//重载前置++运算符
	MyInteger operator++() {
		m_num++;
		return *this;
	}
	//重载后置++运算符
private:
	int m_num;
};
ostream& operator<<(ostream& cout, MyInteger myint) {
	cout << "myint=" << myint.m_num;
	return cout;
}

void test01() {
	MyInteger myint;
	cout << myint << endl;
	cout << ++(++myint) << endl;
	cout << myint << endl;
}
int main() {
	test01();
 }


分析:
其中对cout<<myint<<endl;//输出0
cout<<++(++)myint<<endl;//输出2
cout<<myint<<endl;//输出1
其中对myint的两次++操作只有一次成功了,第二次失败了
原因是当其进行了一次++操作后返回的是一个新的变量,新的对象,再一次的++操作是对新的对象进行的
返回引用是为了一直对一个数据进行++操作
(3).后置++运算符
问题一:假设一个代码同时出现前置++和后置++重载,代码向下面这么写可不可以
//重载前置++运算符
    MyInteger& operator++() {
        m_num++;
        return *this;
    }
    //重载后置++运算符
    MyInteger operator++() {

    }
答:不可以,返回值类型不能区分重载
解决方法:
//重载前置++运算符
    MyInteger& operator++() {
        m_num++;
        return *this;
    }
    //重载后置++运算符
    MyInteger operator++(int) {

    }
      //这里的int代表占位参数,可以用来区分前置和后置递增
问题二:为什么前置的时候返回的是引用,而后置的时候返回值?
//重载后置++运算符
    MyInteger operator++(int) {
        //先记录一下未递增前的结果
        MyInteger temp = *this;
        //后递增
        m_num++;
        //最后将记录的结果返回
        return temp;
    }
如果后置时我们返回引用,相当于我们返回的是一个局部的对象,局部的对象在函数执行完就会被释放掉
如果还要返回引用这将是非法操作
代码案例如下:
 

#include<iostream>
using namespace std;
class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger(){
		m_num = 0;
	}
	//重载后置++运算符
	MyInteger operator++(int) {
		//先记录一下未递增前的结果
		MyInteger temp = *this;
		//后递增
		m_num++;
		//最后将记录的结果返回
		return temp;
	}
private:
	int m_num;
};
ostream& operator<<(ostream& cout, MyInteger myint) {
	cout << "myint=" << myint.m_num;
	return cout;
}

void test01() {
	MyInteger myint;
	cout << myint << endl;
	cout << myint++ << endl;
	cout << myint << endl;
}
int main() {
	test01();
 }

对其分析:
cout << myint << endl;//输出0
cout << myint++ << endl;//输出0
cout << myint << endl;//输出1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值