W
W
WildOne28022020-06-16 16:13:19
C++ / C#
WildOne2802, 2020-06-16 16:13:19

What's the difference between return this and return *this?

The question is what returns

iterator &operator+=(Value_Type idx) {
                ptr_ += idx;
                return *this;
            }
<\code>
в представленном ниже РАБОЧЕМ коде.
<code lang="cpp">


class iterator : public std::iterator<std::random_access_iterator_tag, Value_Type> {
        private:
            Value_Type *ptr_;

        public:
            using difference_type = typename std::iterator<std::random_access_iterator_tag, Value_Type>::difference_type;
            iterator() : ptr_(nullptr) {}
            explicit iterator(Value_Type *ptr) : ptr_(ptr) {}
            iterator(const iterator &it) : ptr_(it.ptr_) {}
            ~iterator() = default;

            iterator &operator+=(Value_Type idx) {
                ptr_ += idx;
                return *this;
            }
            iterator &operator-=(Value_Type idx) {
                ptr_ -= idx;
                return *this;
            }
            iterator operator++() {
                ptr_++;
                return *this;
            }
            iterator &operator--() {
                ptr_--;
                return *this;
            }
            iterator operator++(Value_Type) {
                iterator tmp(*this);
                ++ptr_;
                return tmp;
            }
            iterator operator--(Value_Type) {
                iterator tmp(*this);
                --ptr_;
                return tmp;
            }

            difference_type operator - (const iterator& itr) const { return ptr_ - itr.ptr_; }
            iterator operator + (Value_Type idx) {return iterator(ptr_ + idx);}
            iterator operator - (Value_Type idx) {return iterator(ptr_ - idx);}

            Value_Type &operator*() const { return *ptr_; }
            Value_Type *operator->() const { return ptr_; }
            Value_Type &operator[](const Value_Type idx) { return ptr_[idx]; }

            bool operator == (const iterator & other) const {return other.ptr_ == this->ptr_;}
            bool operator != (const iterator & other) const {return other.ptr_ != this->ptr_;}
            bool operator < (const iterator & other) const {return other.ptr_ < this->ptr_;}
            bool operator > (const iterator & other) const {return other.ptr_ > this->ptr_;}
            bool operator >= (const iterator & other) const {return other.ptr_ >= this->ptr_;}
            bool operator <= (const iterator & other) const {return other.ptr_ <= this->ptr_;}
        };
</code>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Zhilin, 2020-06-16
@WildOne2802

thisis a pointer to the current object. *thisis a reference to the current object. Example:

class A {
public:
  int x;
  A(const A& other) = default;
  A& operator=(const A& other) {
    x = other.x;
    return *this;
  }
};

void test() {
  A a(1);  // a.x == 1
  A b(2);  // b.x == 2
  A c(3);  // c.x == 3

  a = b = c;
  // a.x == 3
  // b.x == 3
  // c.x == 3
}

How does this assignment work? The assignment operator is right-associative, meaning the compiler sees it as a = (b = c);. So, first b = c. Called b.operator=(c). There, we first assign c.x(3) to b.x, then (and this is the most important thing here!) We return a reference to b. Further, the same returned reference to b is assigned to the a = (результат)object , due to which 3 also turns out to be in the object. aa.x

D
Dmitry Pavlov, 2020-06-16
@Stalker31

Returns the object pointed to by the pointer.

N
none7, 2020-06-17
@none7

The *pointer operation originally came from C and was called "pointer dereferencing". Means that all operations will be performed with the value referred to by the pointer. Assigning a value to another variable will create a copy of the value. But in C++ everything was complicated by the introduction of a reference type, the symbol '&'. This is the same pointer, but each operation with it is similar to *(pointer) and, accordingly, pointer arithmetic with references is impossible (without type casting).
In this example, the pointer to the object is dereferenced, but since the returned type is a reference, the type is automatically cast to it. It is logical to assume that as a result of the += operation, copying from the link will occur, but nothing, it is simply thrown away ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question