- #1
in the rye
- 83
- 6
Hi all,
I have a question. I am writing a copy constructor for a class, and I'm not sure this has ever made sense to me. When we write the copy constructor we return *this to support chained assignments. But my question is, why is this required?
Suppose we have integers a,b,c where c = 0. If I assign:
a = b = c I understand that b = c will assign 0 to b then return 0 so that a is assigned 0. However, why is the return value in the constructor required? It seems to me that if we left it out, b=c should be assigned, return nothing, but does this means that the b=c just drops out from the expression entirely so that we have nothing on the rhs?
It just seems like a should be assigned 0 regardless. Since b=c will be assigned 0 and now a is being assigned to b and b is now 0. The return value doesn't seem to be necessary, but obviously I'm wrong and I want to know why.
Thanks.
I have a question. I am writing a copy constructor for a class, and I'm not sure this has ever made sense to me. When we write the copy constructor we return *this to support chained assignments. But my question is, why is this required?
Suppose we have integers a,b,c where c = 0. If I assign:
a = b = c I understand that b = c will assign 0 to b then return 0 so that a is assigned 0. However, why is the return value in the constructor required? It seems to me that if we left it out, b=c should be assigned, return nothing, but does this means that the b=c just drops out from the expression entirely so that we have nothing on the rhs?
It just seems like a should be assigned 0 regardless. Since b=c will be assigned 0 and now a is being assigned to b and b is now 0. The return value doesn't seem to be necessary, but obviously I'm wrong and I want to know why.
Thanks.