I have problems implementing the ++ increment operator

This question already has an answer here:

  • What are the basic rules and idioms for operator overloading? 7 answers

  • operator++() is the prefix increment operator. Implement the postfix operator as operator++(int) .

    The canonical implementations are have the prefix operator return a reference and the postfix operator return by value. Also, you would typically implement the postfix operator in terms of the prefix operator in the interests of least surprise and easy maintenance. Example:

    struct T
    {
     T& operator++()
     {
      this->increment();
      return *this;
     }
    
     T operator++(int)
     {
       T ret = *this;
       this->operator++();
       return ret;
     }
    };
    

    (Increment/decrement operators at cppreference.)

    链接地址: http://www.djcxy.com/p/12710.html

    上一篇: 为什么没有明确返回该函数中指定的返回类型?

    下一篇: 我执行++增量运算符时遇到问题