oczkoisse
oczkoisse

Reputation: 1642

What is the return type of the built-in assignment operator?

I am just starting C++. I am a bit confused about the return type of assignment and dereference operator. I am following the book C++ Primer. At various occasions, the author says that the return type of assignment operator is reference to the type of left hand operand but later on, he says that the return type is the type of the left hand operand. I have referred C++11 Standard Sec. 5.17, where the return type is described as "lvalue referring to left hand operand".

Similarly, I can't figure out whether dereference returns the pointed-to object or the reference to the object.

Are these statements equivalent? If so, then how?

Upvotes: 41

Views: 55548

Answers (4)

sam
sam

Reputation: 856

Chaining Works Without Reference: Returning a copy from operator= still allows for chaining assignments (like a = b = c), as the operator= will return a temporary object (a copy of the assigned object), which can then be used in subsequent assignments. However, this involves creating and destroying temporary objects, which might incur a performance overhead, especially if these operations are expensive for the class in question.

As Scott Meyers mentioned in his Effective C++ book :

The way this is implemented is that assignment returns a reference to its left-hand argument, and that’s the convention you should follow when you implement assignment operators for your classes. This is only a convention; code that doesn’t follow it will compile. However, the convention is followed by all the built-in types as well as by all the types in (or soon to be in — see Item 54) the standard library (e.g., string, vector, complex, tr1::shared_ptr, etc.). Unless you have a good reason for doing things differently, don’t.

Code below works just fine!

#include <iostream>
class Rational {
public:
    // ctor is deliberately not explicit; allows implicit int-to-Rational conversions
    Rational(int numerator = 0, int denominator = 1)
    {

        this->m_numerator = numerator;
        this->m_denominator = denominator;
    }
    int numerator() const  // accessors for numerator 
    {

        return this->m_numerator;
    }
    int denominator() const // accessors for denominator 
    {

        return this->m_denominator;

    }
    Rational operator=(const Rational& rhs)
    {
        std::cout << "operator=(const Rational& rhs)\n";
        this->m_numerator = rhs.m_numerator;
        this->m_denominator = rhs.m_denominator;
        return *this;
    }
    
    private:
    int m_numerator{};
    int m_denominator{};
};

int main()
{
    Rational a(1, 2);
    Rational b(3, 4);
    Rational c(5, 6);
    a = b = c; //  a.operator=(b.operator=(c));
}

Upvotes: 0

SomeWittyUsername
SomeWittyUsername

Reputation: 18358

The standard correctly defines the return type of an assignment operator. Actually, the assignment operation itself doesn't depend on the return value - that's why the return type isn't straightforward to understanding.

The return type is important for chaining operations. Consider the following construction: a = b = c;. This should be equal to a = (b = c), i.e. c should be assigned into b and b into a. Rewrite this as a.operator=(b.operator=(c)). In order for the assignment into a to work correctly the return type of b.operator=(c) must be reference to the inner assignment result (it will work with copy too but that's just an unnecessary overhead).

The dereference operator return type depends on your inner logic, define it in the way that suits your needs.

Upvotes: 46

bash.d
bash.d

Reputation: 13207

I have seen similar issues, but I guess it would be best to use

X& X::operator=(const X&);

Using this, you will be able to reuse the object in a chain-assignment.

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258618

They can both be anything, but usually operator = returns the current object by reference, i.e.

A& A::operator = ( ... )
{
   return *this;
}

And yes, "reference to the type of left hand operand" and "lvalue referring to left hand operand" mean the same thing.

The dereference operator can have basically any return type. It mostly depends on the logic of the program, because you're overloading the operator that applies to an object, not to a pointer to the object. Usually, this is used for smart pointers, or iterators, and return the object they wrap around:

struct smart_ptr
{
   T* innerPtr;
   T* smart_ptr::operator* ()
   {
      return innerPtr;
   }
}

smart_ptr p; 
T* x = *p;  

Upvotes: 13

Related Questions