Copy reference operator returning a reference not an object using the keyword 'this'

Viewed 74

I want to ask a question about copy reference operators.

I have the following class named Mystring and I have a copy reference operator for it, which works.

#ifndef _MY_STRING_
#define _MY_STRING_

class Mystring
{
private:
    char *str; // pointer to a char[] that holds a c-style string
public:
    Mystring(); // no args
    Mystring(const char *s); // overloaded
    Mystring(const Mystring &source); // copy
    ~Mystring(); // destructor

    Mystring &operator=(const Mystring &rhs);// overloaded copy assignment operator

    void display() const; // getters
    int get_length() const;
    const char *get_str() const; // pointer is returned

};

#endif // _MY_STRING_

and this is the copy reference operator function:

// Overloaded assignment copy operator
Mystring &Mystring::operator = (const Mystring &rhs) {
    std::cout << "Copy assignment" << std::endl;
    if (this == &rhs) // check the pointer is on the same address of rhs i.e. & = reference operator
        return *this; // returns the reference
    delete [] this->str;
    str = new char [std::strlen(rhs.str) + 1];
    std::strcpy(this->str, rhs.str);
    return *this;
}

I'm a beginner in C++ so this is confusing but I'll try to describe what happens.

I'm aware that there is a this operator, which acts as a pointer. When dereferenced, it allows us to work with the object itself.

From the first line

Mystring &Mystring::operator = (const Mystring &rhs)

I can see that I will have an operator function that returns a reference, as the & operator exists in the declaration.

However, at the end of the if statement and the overall function, we state

return *this

although if we are dereferencing this, we're returning the object and not the reference as per my explanation above.

Microsoft c++ documentation also states

The expression *this is commonly used to return the current object from a member function:

To clarify, this is a pointer to a current object so *this and then returning it should mean that i'm returning the object, not another reference.

What mistake am I making in my understanding of this code?

2 Answers

In C++ there are expressions whose evaluation just determines the identity of an object. They are called glvalue expressions. This is the case of the expression *this. this hold a value that represents an address in memory and the expression *this evaluates to the identity of the object that is at that address. This may be strange to call this an evaluation, this is just a designation: *this designates the object that is at the address hold by this.

A function that returns a reference, when called result in a glvalue: it just designates an object. In the case of the operator = this object is the one designated by the expression in the return statement *this.

To clarify, this is a pointer to a current object so *this and then returning it should mean that i'm returning the object, not another reference.

You are correct, this is the pointer and *this is the object the pointer points to.

Take a look, however, on how you deal with references. For example consider passing a value to a function by reference. It is done in a following way:

void foo(char &param) {
  // do something
}

int main(int argc, char **argv) {

    int x = 5;
    
    foo(x);
    
    return 0;
}

Note that the variable x is passed by reference but there is no & or * needed to indicate that to the compiler. This is because the compiler can figure it out by itself. This is also why you return *this and it is understood as reference.

Related