【Cpp】运算符重载 | 前置++(--)# 后置++(--)

标题:【Cpp】运算符重载 | 前置++(--)# 后置++(--)

@水墨不写bug



正文开始:

        对于内置类型的前置后置++(--)我们已经很清楚了:

        前置++(--)先++(--)再使用,后置++(--)先使用再++(--);

特别的:

        对于指针类型,++(--)表示向前或者向后跳过这个指针指向的一个数据类型,例如对于int型,表示向前或者向后跳过sizeof(int)字节的地址,其他的指针类型类似。

        本文主要内容是对自定义类型的前置后置自增自减运算的讨论,实现方式和相关语法规则。 

        编译器对于内置类型的自增自减运算已经有明确的定义,但是对于对象(自定义类型),它的自增自减运算没有明确的定义,它的定义取决于你的(自定义类型)对象的具体使用场景是什么,以及你想 使 对象 通过自增自减来达到什么样的功能。

举一个例子:

        比如你实现的对象是一个日期的类,日期的自增自减操作就是日期的向前向后推移;

        再比如说你实现了一个学籍系统,你的对象是一个学生的类,那么学生的自增自减看似没有什么实际意义,但是你可以赋予它意义,通过定义运算符重载函数,学生的自增自减也许就是学生的学号的向前向后推移。


         本文以日期类的运算符重载为例子,讨论自增自减运算符的重载的实现。

这里直接给出日期类的头文件:


class Date
{
public:
	friend ostream& operator<<(ostream& out, const Date d);

	// 获取某年某月的天数
	inline int GetMonthDay(int year, int month)
	{
		assert(month >= 1 && month <= 12);
		static int _month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//如果进入if说明是闰年并且是二月
		if (month == 2 && ( (year % 4 == 0) && (year % 100 != 0) ) || (year % 400 == 0))
		{
			return 29;
		}
		else
			return _month[month];
	}
	// 全缺省的构造函数
	//声明时写缺省值
	Date(int year = 1900, int month = 1, int day = 1);

	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);

	// 析构函数
	~Date();


	// 日期+=天数
	Date& operator+=(int day);

	// 日期+天数
	Date operator+(int day);

	// 日期-天数
	Date operator-(int day);

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++
	Date operator++(int);

	// 后置--
	Date operator--(int);

	// 前置--
	Date& operator--();

	// >运算符重载
	bool operator>(const Date& d);

	// ==运算符重载
	bool operator==(const Date& d);

	// >=运算符重载
	bool operator >= (const Date& d);

	// <运算符重载
	bool operator < (const Date& d);

	// <=运算符重载
	bool operator <= (const Date& d);

	// !=运算符重载
	bool operator != (const Date& d);

	// 日期-日期 返回天数
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

(一)前置++(--)

        对前置++(--)运算符进行重载时,根据运算符重载规则:


	// 前置++
	Date& operator++();

    // 前置--
    //
    Date& Date::operator--()

        对日期的自增就是日期天数加一,向后推移一天。

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

// 前置--
//
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

(二)后置++(--)

        对于后置++(--)的重载就遇到问题了:

        前置重载已经把名字给占用了,后置不就会因为函数重名,并且不满足重载规则而无法重载吗?

        为了解决这一问题,Cpp规定,对于后置自增自减运算符,在参数列表中加一个整形参数作为标记,这样由于满足了Cpp的函数重载规则,于是后置自增自减运算符就可以重载了。


//先使用再++
// 后置++
Date Date::operator++(int)
{
	Date tem = *this;
	*this += 1;
	return tem;
}

// 后置--
Date Date::operator--(int)
{
	Date tem;
	*this -= 1;
	return tem;
}

        为了满足后置这一“功能”,在具体实现时,拷贝构造一个tem对象,作为操作符返回值,返回上一级函数使用,也就是说,上一级函数实际使用的不是this对象自增自减后的值,而是拷贝构造的tem对象的值。

(三)总结

         对于自定义类型的自增自减重载,本来函数是重名的,逻辑上无法构成重载(函数名称相同,参数列表完全一致)。但是为了构成重载,Cpp规定:

        1.前置自增自减按原方法写;

        2.后置自增自减在参数列表中加一个整形变量以示区分。


完~

未经作者同意禁止转载 

  • 43
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
为实现复数类的++ --运算,我们需要对复数类进行运算符重载。我们可以重载前置++和--运算符和后置++和--运算符。 下面是一个简单的复数类的定义: ```cpp class Complex { private: double real_; double imag_; public: Complex(double real = 0, double imag = 0) : real_(real), imag_(imag) {} double real() const { return real_; } double imag() const { return imag_; } Complex operator++(); //前置++ Complex operator--(); //前置-- Complex operator++(int); //后置++ Complex operator--(int); //后置-- }; Complex Complex::operator++() { real_++; imag_++; return *this; } Complex Complex::operator--() { real_--; imag_--; return *this; } Complex Complex::operator++(int) { Complex temp(*this); real_++; imag_++; return temp; } Complex Complex::operator--(int) { Complex temp(*this); real_--; imag_--; return temp; } ``` 前置++和--运算符直接将实部和虚部分别加/减1即可。 后置++和--运算符需要先保存当前对象的值,然后再将实部和虚部分别加/减1,最后返回保存的对象值。 下面是一个使用复数类的示例: ```cpp int main() { Complex a(1, 2); Complex b = ++a; Complex c = b--; cout << "a=" << a.real() << "+" << a.imag() << "i" << endl; cout << "b=" << b.real() << "+" << b.imag() << "i" << endl; cout << "c=" << c.real() << "+" << c.imag() << "i" << endl; return 0; } ``` 输出结果为: ``` a=2+3i b=2+3i c=3+4i ``` 可以看到,前置++后置--运算符都可以正常工作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水墨不写bug

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

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

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

打赏作者

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

抵扣说明:

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

余额充值