How, exactly, are chained assignments processed?

  • Thread starter Thread starter in the rye
  • Start date Start date
Click For Summary
SUMMARY

The discussion clarifies the distinction between a copy constructor and an assignment operator in C++. The copy constructor does not return a value, while the assignment operator must return a reference to the current object (i.e., *this) to facilitate chained assignments. The example provided illustrates how chained assignments work, specifically with integers and floats, emphasizing that the assignment operator's return value is crucial for maintaining the integrity of the assignment chain. Misunderstanding this concept can lead to confusion regarding object assignments in C++.

PREREQUISITES
  • Understanding of C++ class constructors and destructors
  • Knowledge of C++ assignment operators
  • Familiarity with C++ reference types
  • Basic understanding of variable assignment and chaining in programming
NEXT STEPS
  • Study C++ copy constructors and their implementation
  • Learn about C++ assignment operators and their role in object assignment
  • Explore the concept of chaining assignments in C++
  • Review best practices for implementing operator overloading in C++
USEFUL FOR

C++ developers, software engineers, and computer science students looking to deepen their understanding of object-oriented programming concepts, specifically regarding constructors and 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;
}
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
89
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
18
Views
3K
  • · Replies 23 ·
Replies
23
Views
4K