Why do I need to dereference this when returning a class object reference? C++

Why do I need to dereference this when returning a class object reference ? Example:

class Point
{
private:
    int x;
    int y;
public:
    Point& operator= (const Point &other)
    {
        this->x = other.x;
        this->y = other.y;
        return *this;
    }
}

The return type of the method is = Point&, and we return *this

After all, a link stores an address, just like a pointer, but then why do we dereference the this pointer ?

Author: Repsys, 2018-01-27

3 answers

First, in C++, a link does not "store an address". How the link is internally arranged is not specified by the language. Reference types in C++ are not object types - they are not formally required to occupy memory and, accordingly, they are not required to" store " anything. As stated in Stroustrup's TC++PL, one of the acceptable informal ways to represent references is to think of them as alternative names for existing objects (or functions).

Second, the type of expression this in your case is Point *, and the return type of your function is Point &. These are completely different types. The C++ language will not think for you. what exactly did you mean by trying to return Point * where Point & is required. In this case, it is your task to observe the type correspondence.

 4
Author: AnT, 2018-01-27 22:05:07
  Point a, b, c;
    a = b;

Here you perform the assignment b, and a becomes such as b (to make it easier for you to understand), and it is this object that you need after the assignment (*this for a this is a), the second reason for the expediency of returning the reference, and not a copy, is the ability to write the following:

b = c = a; 

(b = c) this is a reference to b, which is assigned to a; (c = a) this is a reference to c , etc.
I will also add that the link can not store anything in itself, since the link is just a name, it does not take up the memory area to store something else...

 1
Author: AR Hovsepyan, 2018-01-27 17:58:37

In fact, a reference and a pointer are functionally really the same thing. Even your "keeps to yourself" doesn't apply here. In fact, in any implementation, a reference and a pointer are no different. And I don't understand people who suggest artificially thinking of the link as some kind of spherical horse in a vacuum.

But syntactically, from the point of view of the language, these things are different. Actually, the reference was introduced into the C++ language because in some cases it is more convenient with it than with a pointer. We can say that it's just a different way of looking at the same thing.

Never forget that the computer does not know how to think, it always does only what it is told, and with accuracy to every letter.

In this case, the developers of the language were simply lazy to make an exception for a single case, when in principle there are not particularly different interpretations. It's much easier to get everyone to specify dereferencing. So just accept it and remember, without asking why.

 0
Author: reshu, 2018-01-27 19:56:50