How, exactly, are chained assignments processed?

  • Thread starter Thread starter in the rye
  • Start date Start date
AI Thread Summary
The discussion centers on the distinction between copy constructors and assignment operators in C++. The original poster questions the necessity of returning *this in an assignment operator to support chained assignments, using the example of integers a, b, and c. They express confusion about why the return value is required for the assignment to work as expected, suggesting that if the return were omitted, the assignment should still function correctly.A key clarification is provided that the copy constructor does not return anything, while the assignment operator does. The example given illustrates the structure of a class with a copy constructor and an assignment operator, emphasizing that the assignment operator must return *this to allow for expressions like a = b = c to work properly. Additionally, the discussion touches on the use of assignment results in conditional statements, further highlighting the importance of return values in assignment operations.
in the rye
Messages
83
Reaction score
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
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;
 
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;
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top