C++自增自减运算符重载int参数的问题

解决为什么自增自减运算符重载带int参数就是后置,不带就是前置的疑问

转自:http://dev.yesky.com/228/2578228.shtm

很久以前(八十年代),没有办法区分++和--操作符的前缀与后缀调用。这个问题遭到程序员的报怨,于是C++语言得到了扩展,允许重载increment 和 decrement操作符的两种形式。

  然而有一个句法上的问题,重载函数间的区别决定于它们的参数类型上的差异,但是不论是increment或decrement的前缀还是后缀都只有一个参数。为了解决这个语言问题,C++规定后缀形式有一个int类型参数,当函数被调用时,编译器传递一个0做为int参数的值给该函数:

class UPInt { // "unlimited precision int"
public:
 UPInt& operator++(); // ++ 前缀
 const UPInt operator++(int); // ++ 后缀
 UPInt& operator--(); // -- 前缀
 const UPInt operator--(int); // -- 后缀
 UPInt& operator+=(int); // += 操作符,UPInts
 // 与ints 相运算
 ...
};

UPInt i;

++i; // 调用 i.operator++();
i++; // 调用 i.operator++(0);
--i; // 调用 i.operator--();
i--; // 调用 i.operator--(0);

  这个规范有一些古怪,不过你会习惯的。而尤其要注意的是这些操作符前缀与后缀形式返回值类型是不同的。前缀形式返回一个引用,后缀形式返回一个const类型。下面我们将讨论++操作符的前缀与后缀形式,这些说明也同样使用与--操作符。

  从你开始做C程序员那天开始,你就记住increment的前缀形式有时叫做“增加然后取回”,后缀形式叫做“取回然后增加”。这两句话非常重要,因为它们是increment前缀与后缀的形式上的规范。

// 前缀形式:增加然后取回值

UPInt& UPInt::operator++()
{
 *this += 1; // 增加
 return *this; // 取回值
}

// postfix form: fetch and increment

const UPInt UPInt::operator++(int)
{
 UPInt oldValue = *this; // 取回值
 ++(*this); // 增加
 return oldValue; // 返回被取回的值
}

  后缀操作符函数没有使用它的参数。它的参数只是用来区分前缀与后缀函数调用。如果你没有在函数里使用参数,许多编译器会显示警告信息,很令人讨厌。为了避免这些警告信息,一种经常使用的方法时省略掉你不想使用的参数名称;如上所示。

  很明显一个后缀increment必须返回一个对象(它返回的是增加前的值),但是为什么是const对象呢?假设不是const对象,下面的代码就是正确的:

UPInt i;
i++++; // 两次increment后缀
// 运算

  这组代码与下面的代码相同:

i.operator++(0).operator++(0);

  很明显,第一个调用的operator++函数返回的对象调用了第二个operator++函数。

  有两个理由导致我们应该厌恶上述这种做法,第一是与 内置类型行为不一致。当设计一个类遇到问题时,一个好的准则是使该类的行为与int类型一致。而int类型不允许连续进行两次后缀increment:

int i;
i++++; // 错误!

  第二个原因是使用两次后缀increment所产生的结果与调用者期望的不一致。如上所示,第二次调用operator++改变的值是第一次调用返回对象的值,而不是原始对象的值。因此如果:

i++++;

  是合法的,i将仅仅增加了一次。这与人的直觉相违背,使人迷惑(对于int类型和UPInt都是一样),所以最好禁止这么做。

  C++禁止int类型这么做,同时你也必须禁止你自己写的类有这样的行为。最容易的方法是让后缀increment 返回const对象。当编译器遇到这样的代码:

i++++; // same as i.operator++(0).operator++(0);

  它发现从第一个operator++函数返回的const对象又调用operator++函数,然而这个函数是一个non-const成员函数,所以const对象不能调用这个函数。如果你原来想过让一个函数返回const对象没有任何意义,现在你就知道有时还是有用的,后缀increment和decrement就是例子。(更多的例子参见Effective C++ 条款21)

  如果你很关心效率问题,当你第一次看到后缀increment函数时, 你可能觉得有些问题。这个函数必须建立一个临时对象以做为它的返回值,(参见条款19),上述实现代码建立了一个显示的临时对象(oldValue),这个临时对象必须被构造并在最后被结构。前缀increment函数没有这样的临时对象。由此得出一个令人惊讶的结论,如果仅为了提高代码效率,UPInt的调用者应该尽量使用前缀increment,少用后缀increment,除非确实需要使用后缀increment。让我们明确一下,当处理用户定义的类型时,尽可能地使用前缀increment,因为它的效率较高。

  我们再观察一下后缀与前缀increment 操作符。它们除了返回值不同外,所完成的功能是一样的,即值加一。简而言之,它们被认为功能一样。那么你如何确保后缀increment和前缀increment的行为一致呢?当不同的程序员去维护和升级代码时,有什么能保证它们不会产生差异?除非你遵守上述代码里的原则,这才能得到确保。这个原则是后缀increment和decrement应该根据它们的前缀形式来实现。你仅仅需要维护前缀版本,因为后缀形式自动与前缀形式的行为一致。

  正如你所看到的,掌握前缀和后缀increment和decrement是容易的。一旦了解了他们正确的返回值类型以及后缀操作符应该以前缀操作符为基础来实现的规则,就足够了。
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 中,我们可以通过重载运算符来实现日期之间的差值计算。具体步骤如下: 1. 定义一个 Date 类,包含年、月、日等属性,并实现自增自减运算符重载。 ```c class Date { public: Date(int year, int month, int day): year_(year), month_(month), day_(day) {} Date& operator++(); // 自增运算符重载 Date operator++(int); // 后置自增运算符重载 Date& operator--(); // 自减运算符重载 Date operator--(int); // 后置自减运算符重载 private: int year_; int month_; int day_; }; ``` 2. 在重载运算符的实现中,我们需要注意日期的进位和借位问题。例如,当月份为 12 时,年份需要进位;当日期为 1 时,月份需要借位。 ```c Date& Date::operator++() { day_++; if (day_ > days_of_month(year_, month_)) { day_ = 1; month_++; if (month_ > 12) { month_ = 1; year_++; } } return *this; } Date Date::operator++(int) { Date temp(*this); ++(*this); return temp; } Date& Date::operator--() { day_--; if (day_ < 1) { month_--; if (month_ < 1) { month_ = 12; year_--; } day_ = days_of_month(year_, month_); } return *this; } Date Date::operator--(int) { Date temp(*this); --(*this); return temp; } ``` 3. 定义一个 days_between 函数,用于计算两个日期之间的天数差值。该函数可以通过调用重载自增自减运算符来实现。 ```c int days_between(const Date& d1, const Date& d2) { Date d(d1); int count = 0; while (d != d2) { if (d2 > d) { ++d; count++; } else { --d; count--; } } return count; } ``` 4. 注意,我们还需要实现一个 days_of_month 函数,用于计算给定年份和月份的天数。 ```c int days_of_month(int year, int month) { static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) { return 29; } return days[month - 1]; } bool is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } ``` 完整代码如下: ```c #include <iostream> class Date { public: Date(int year, int month, int day): year_(year), month_(month), day_(day) {} Date& operator++(); // 自增运算符重载 Date operator++(int); // 后置自增运算符重载 Date& operator--(); // 自减运算符重载 Date operator--(int); // 后置自减运算符重载 bool operator==(const Date& other) const { return year_ == other.year_ && month_ == other.month_ && day_ == other.day_; } bool operator!=(const Date& other) const { return !(*this == other); } bool operator<(const Date& other) const { if (year_ < other.year_) return true; if (year_ > other.year_) return false; if (month_ < other.month_) return true; if (month_ > other.month_) return false; return day_ < other.day_; } bool operator>(const Date& other) const { return other < *this; } bool operator<=(const Date& other) const { return !(other < *this); } bool operator>=(const Date& other) const { return !(*this < other); } private: int year_; int month_; int day_; }; int days_of_month(int year, int month) { static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) { return 29; } return days[month - 1]; } bool is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } Date& Date::operator++() { day_++; if (day_ > days_of_month(year_, month_)) { day_ = 1; month_++; if (month_ > 12) { month_ = 1; year_++; } } return *this; } Date Date::operator++(int) { Date temp(*this); ++(*this); return temp; } Date& Date::operator--() { day_--; if (day_ < 1) { month_--; if (month_ < 1) { month_ = 12; year_--; } day_ = days_of_month(year_, month_); } return *this; } Date Date::operator--(int) { Date temp(*this); --(*this); return temp; } int days_between(const Date& d1, const Date& d2) { Date d(d1); int count = 0; while (d != d2) { if (d2 > d) { ++d; count++; } else { --d; count--; } } return count; } int main() { Date d1(2020, 1, 1); Date d2(2020, 1, 10); std::cout << "Days between " << d1 << " and " << d2 << ": " << days_between(d1, d2) << std::endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值