Why can't I add pointers?

前端 未结 7 872
终归单人心
终归单人心 2020-11-30 00:38

I have code very similiar to this:

LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator+(const Iterator& right)const
{
    return (this + &am         


        
相关标签:
7条回答
  • 2020-11-30 01:42

    Other answers explained already why, what You are doing doesn't work, but my guess is, that You want to define typical operator+ for an iterator, but got lost in that attempt.

    Both pointers and standard random access iterators allow to advance the pointer or iterator by an integral value. In case of iterators, an operator+ is defined, that takes an integral value as an argument and returns an iterator.

    LINT_rep::Iterator LINT_rep::Iterator::operator+(int distance) const;
    

    You can define such operator as a method, but this method will allow You to write

    iterator + distance
    

    but not

    distance + iterator
    

    To make the addition commutative You have to define a friend non-member function that takes the distance as the first parameter and an iterator object as a second

    friend LINT_rep::Iterator LINT_rep::Iterator::operator+(int distance, const  LINT_rep::Iterator & rhs);
    
    0 讨论(0)
提交回复
热议问题