c++ - 为什么这样不能实现后置递增?
伊谢尔伦
伊谢尔伦 2017-04-17 13:02:55
0
2
470
        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*)

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
伊谢尔伦

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

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

洪涛

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template