1

Look here:

class IntClass
{
public:
IntClass operator=(const IntClass& rhs);
IntClass();
~IntClass();
int value;
};

Implementation:

IntClass::IntClass()
{
}
IntClass::~IntClass()
{
}
IntClass IntClass::operator=(const IntClass& rhs)
{
 this->value = rhs.value;
 return *this;
}

As you can see I am not returning a reference to the class. From the examples of overloading the = operator I see that people return a reference. Why is this?

This still functions as intended:

IntClass a, b, c, d;
a.value = 20;
b = c = d = a;
printf("%d", b.value);
printf("%d", c.value);
printf("%d", d.value);

Output: 202020

p.s I know the public access to value is bad.

T.C.
139k18 gold badges306 silver badges440 bronze badges
asked Aug 28, 2014 at 23:22
6
  • Returning a value gives you a copy. Commented Aug 28, 2014 at 23:27
  • and returning a reference returns the actual data-field. Thats about it. Google would'ne answered that in 2 seconds. Commented Aug 28, 2014 at 23:29
  • Yes, but if I am copying the value from rhs to the instance it shouldn't matter. The only thing I can think of is if this is a huge class passing a new copy would be expensive. A reference would be cheaper. Commented Aug 28, 2014 at 23:29
  • indeed, thats about the reason. Plus : in exotic environments one might want to lock on shared memory-pages, which are then modified in turn by several threads ... its the most efficient way, simple as that. Commented Aug 28, 2014 at 23:32
  • 5
    Returning reference makes the result of assignment an lvalue (mimics the built-in assignment operator). Returning a copy makes the result an rvalue. This can have interesting consequences. For example, void f(IntClass &x); IntClass x, y; f(x = y); only compiles if your operator= returns a reference. Commented Aug 28, 2014 at 23:32

1 Answer 1

2

First, having your assignment operator return by value means that you are making an extra copy. The copy can be expensive, and compilers usually cannot elide it. If we assume that copy assignment and copy construction have roughly the same cost, then returning by value basically doubles the cost of the assignment.

Second, if your assignment operator return a reference, the result of the assignment expression is an lvalue, which mimics the built-in assignment operator. If your assignment operator return by value, the result of the expression is an rvalue. This can have interesting consequences:

void f(IntClass &lv); 
void g(IntClass &&rv);
void g(const IntClass &clv);
IntClass x, y; 
y.value = 10;
f(x = y); // compiles only if operator= returns IntClass & 
 // as non-const lvalue references cannot bind to a temporary
g(x = y); // calls g(IntClass &&) if operator= returns IntClass
 // calls g(const IntClass &) if operator= returns IntClass &
answered Aug 28, 2014 at 23:49
Sign up to request clarification or add additional context in comments.

2 Comments

Returning a reference implies that the object must exist after exiting the function, such as a static variable in a function.
@ThomasMatthews I don't think that's particularly relevant for assignment operators.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.