tags:

views:

119

answers:

4

I went over making my own copy constructor and it overall makes sense to me. However, on the topic of doing your own assignment operator I need someone to fill in the blank for me.

I pretty much don't get why you are returning *this in all the examples, such as the one below:

Foo & Foo::operator=(const Foo & f)
{

//some logic

return *this;

}

So if I have some statements like:

Foo f;
f.hour = 7;

Foo g;
g = f;

Once the assignment operator runs, it returns a reference to the g object (the *this). So now the question is, won't I now have a statement implicitly like this?:

g = g (g being a reference)

The thing is, before, setting a reference to just an object would have caused the copy constructor to be invoked. In this case, it doesn't even though it fits the signature of the copy constructor.

+7  A: 

You want to return *this so you can chain =:

Foo f, g, h;

f = g = h;

This is basically assigning h into g, then assigning g (returned by return *this) into f:

f = (g = h);

Another situation this is sometimes used in is having an assignment in a conditional (considered bad style by many):

if ( (f = 3).isOK() ) {

With the statement g = f; the return is just ignored, like if you did 3 + 4;.

orangeoctopus
Or the more slightly more classic example `((f = func_returning_f()) != f_to_ignore)`
R Samuel Klatchko
+2  A: 

I think you're confusing the idea of infix notation a bit. g = f does not mean call operator= of g with f and put the result into g it means apply operator= on g with parameter f and set the value of this expression to the result. This is identical behavior to the other infix operators such as + or /.

Operators in some languages can be applied like a normal function (and in C++ with some extraneous syntax) such as = (g f) which shows the concept a bit more clearly.

Edit:

For example, what is often used as an example for IO:

// loop until some sentinel value
while ((nextChar = (char) getchar()) != 'Q') {
  string += nextChar;
}

Notice that because the = operator returns its first argument (nextChar in this example) you can compose the assignment and testing.

Ron Warholic
A: 

Once the assignment operator runs, it returns a reference to the g object (the *this). So now the question is, won't I now have a statement implicitly like this?:

g = g (g being a reference)

No, but self assignment is something you need to check for. What returning *this does is make the value of the expression (g=f), g. Which is useful for chaining assignments. In a statement like

Foo a, b, c;
a = b = c;

a is being assigned the return value from the method operator=. Returning *this makes this statement do what is expected (the value of c is assigned to b, then the new value of b is assigned to a).

Graphics Noob
A: 

The reason is so that the result of your assignment can be an rvalue in another expression.

sheepsimulator
The call of a function returning a reference is actually an **lvalue**.
FredOverflow
@FredOverflow: If f is a function returning a reference, then f()=5 is using f() as an lvalue. But what about a=f(); Is this using the f() result as an rvalue? a = b = c is like a = (b = c), which is analogous to a = f()
Aaron McDaid
You have linked to a C question, but the terms *lvalue* and *rvalue* have different meanings in C and C++. (Actually, there is no such thing as an "rvalue" in the current C standard.)
FredOverflow
@FredOverflow: This question/accepted answer would suggest you are incorrect: http://stackoverflow.com/questions/2038414/lvalue-and-rvalue. I think, though, that this question has motivated me to make sure I have a good+solid definition on what an lvalue and an rvalue is (from the answer in this comment, it still seems nebulous to me).
sheepsimulator
@FredOverflow - This might be a stupid question but - where can I find an official definition on lvalue and rvalue in a book/site? I've looked in the indexes of K do you need the actual spec for the definition?
sheepsimulator
Sadly, not a single book I own gets it right :) You must read the standard, for example the [C++0x FCD](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf). But beware, the contained definition of rvalues and temporaries in the FCD is [controversial](http://stackoverflow.com/questions/3007728/rvalues-and-temporary-objects-in-the-fcd).
FredOverflow