3

So this is an example [extracted from Bjarne Stroustrup's "A tour of C++ (2nd edition)] of a copy assignment of a user defined vector class:

Vector& Vector::operator=(const Vector& a) // copy assignment
{
    double* p = new double[a.sz];
    for (int i=0; i!=a.sz; ++i)
        p[i] = a.elem[i];
    delete[] elem; // delete old elements
    elem = p; // here elem is the vector's data holding member array
    sz = a.sz;
    return *this;
}

'This' is a pointer, so dereferencing it should actually give us the current object it points at. How does an object gets accepted as a return value in a case when a reference to said object is expected?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
dreamer
  • 161
  • 1
  • 8

3 Answers3

4

'This' is a pointer, so dereferencing it should actually give us the current object it points at.

Yep.

How does an object gets accepted as a return value in a case when a reference to said object is expected?

The same way any other reference is made.

int x = 3;
int& ref = x;  // just fine

Why does 'this' need to be dereferenced in this case? (assignment operator)

The need to dereference a pointer to obtain the pointee, has very little to do with the function returning a reference type, other than to say that if you didn't dereference the pointer, you'd have to be returning the pointer, so the return type would have to be Vector*.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

this is a pointer that points to the object that is assigned. The function returns a reference to the object. So you need to dereference the pointer to get the lvalue of the object.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

this is a pointer type. Your function returns a reference to a Vector. To convert a pointer to a lvalue, you need to dereference it.

  • 2
    Technically a dereference does not give you an expression of reference type; it gives you an lvalue that refers [hah!] to the original value, and the type of that expression is `Vector` (not `Vector&`). But references are funny old things anyway... – Lightness Races in Orbit Jul 26 '19 at 12:20
  • You changed the wrong sentence :P The function returns a `Vector&` as that's the return type. But you said that dereferencing a pointer produces a reference, which is not true. (It doesn't vastly matter though) – Lightness Races in Orbit Jul 26 '19 at 16:35
  • And that's why I write things like "fixed, I think." That way I'm sure I understand what people are telling me. Okay, should be fixed for real this time. Please double check again and make sure I didn't mess up again. –  Jul 26 '19 at 16:40
  • 1
    Well the value category doesn't quite cover what dereferencing does, but close enough. – Lightness Races in Orbit Jul 26 '19 at 16:54
  • No, you'd need [this question](https://stackoverflow.com/questions/11347111/dereferencing-a-pointer-when-passing-by-reference) to do that properly :D. Anyway, thanks for the friendly feedback. –  Jul 26 '19 at 16:57
  • Heh, yes. And ironically you've found a perfect dupe for this question – Lightness Races in Orbit Jul 26 '19 at 16:58