search

Home  >  Q&A  >  body text

c++ - 为什么这样不能实现后置递增?

        reference operator*() { return *_ptr; }  //解引用操作
        array_iterator<T, N>&operator++()       //前置递增
        {                    
            ++**this;    //增加_ptr,
            //当把上面那句改为  ++_ptr后就能实现  这两者的区别是什么?
            return *this;     
        }
        array_iterator<T, N> operator++(int) //后置递增
        {
            array_iterator<T, N> tmp = *this;    //返回量
            ++*this;
            return tmp;
        }

当把++**this;改为 ++_ptr就能实现,这两者有什么区别?
(_ptr是类里的一个成员,为指针类型 T*)

伊谢尔伦伊谢尔伦2803 days ago474

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:04:55

    ++*this
    == ++(this->operator*())
    == ++*(this->_ptr)
    == ++*_ptr

    ++*_ptr and ++_ptr are obviously different, so of course your code cannot realize your wish.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:04:55

    reference operator() { return _ptr; } //The dereference operation
    the appeal statement returns the value pointed to by ptr,
    and this gets the class, which then When calling the operator above, you get the value pointed to by the pointer, so the value pointed to by the pointer is actually modified instead of the value of the pointer.

    reply
    0
  • Cancelreply