How, exactly, are chained assignments processed?

  • Thread starter in the rye
  • Start date
In summary, the copy constructor is required because the return value is necessary for chained assignments.
  • #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.
 
Technology news on Phys.org
  • #2
in the rye said:
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.
How about this:
C:
float a, c;
int b;

a = b = c = 1.3f;
 
  • #3
First off, be careful with terminology.

The copy constructor does not return anything. You are talking about an assignment operator and they are different.

Code:
struct foo {
   foo(const foo& other) x(other.x){}  //This is the copy constructor
   foo & operator = (const foo & other){ x = other.x;  return *this; }  //This is an assignment operator
};
We also occasionally use the results of assignments to check the value within to know whether to do into another scope.
Code:
if (FILE * fp = fopen("/tmp/somefile", "a")){
   fwrite(fp, 1,1, "a");
} else {
   std::cerr << "Could not open file" << std::endl;
}
 

1. How are chained assignments processed?

Chained assignments are processed from right to left. This means that the value on the right side of the assignment is assigned to the variable on the left, and then the value of that variable is assigned to the next variable on the left, and so on.

2. What happens if one of the variables in a chained assignment is already defined?

If one of the variables is already defined, its value will be overwritten by the value on the right side of the assignment. This value will then be passed on to the next variable on the left, and so on.

3. Can I chain more than three variables in an assignment?

Yes, you can chain as many variables as you want in an assignment. There is no limit to the number of variables that can be chained together.

4. How does chaining assignments affect the performance of my code?

Chaining assignments does not have a significant impact on the performance of your code. In fact, it can sometimes even improve performance, as it reduces the number of lines of code and can make the code more concise.

5. Are there any potential pitfalls to be aware of when using chained assignments?

One potential pitfall to be aware of is accidentally reusing a variable name in a chained assignment. This can lead to unexpected results and can be difficult to debug. It is important to use unique variable names to avoid this issue.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
Replies
10
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Replies
9
Views
1K
Back
Top