-1

I am new to C++ and trying to learn it the proper way. In particular, I am practicing a simple assignment operator overloading in the following class

class Point { public: int x; int y; Point():x(0),y(0){}; Point operator=(Point Y); }; 

The assignment operator being:

Point Point::operator=(Point Y){ Point temp; temp.x = Y.y; temp.y = Y.x; std::printf("temp has coordinates %d, %d\n",temp.x,temp.y); return temp; } 

When I have the following in main, I don't understand why I don't end up with B.x = 0, B.y = 3.

int main() { Point A; Point B; A.x = 3; B = A; std::printf("B has coordinates %d, %d\n",B.x,B.y); return 0; } 

I know I am not using the implicit this pointer to B in my case, and the code does superfluous copies and there is a better way to do it via references as the parameter type. But temp has the fields: temp.x = 0, temp.y = 3, so why doesn't B end up with them?

6
  • 2
    You never assigned the values to B. You assigned the values from A to temp instead. Why did you do that? In the assignment operator you set destination member to the value of source members Commented Apr 12, 2024 at 12:59
  • The compiler generated operator= will do the right thing. Do you need the std::printf side-effect? Commented Apr 12, 2024 at 13:00
  • 1
    I would not call that class Point if the swap x/y is intended. Commented Apr 12, 2024 at 13:11
  • 1
    Point Point::operator=(Point Y), is there a reason you pass the input by value (making a copy)? Or did you intend to pass by reference? Also when you do operator overloading it is customary to do it in such a way that there are "no surprises"... swapping x and y is very very surprising Commented Apr 12, 2024 at 13:21
  • Why must the copy assignment operator return a reference/const reference? Commented Apr 12, 2024 at 13:42

1 Answer 1

3

Your assignment operator does not change the object for which it is called. It creates a temporary object within the assignment operator data members of which are assigned and this temporary object is returned from the assignment operator and is not used in the program.

To make it more clear you can equivalently rewrite this statement

B = A; 

the following way

B.operator =( A ); 

The object for which the operator is called stays unchanged. Its data members were not changed.

The assignment operator should look the following way

class Point { public: int x; int y; Point():x(0),y(0){}; Point & operator =( const Point & ); }; 

and

Point & Point::operator=( const Point &Y ) { x = Y.y; y = Y.x; //std::printf("temp has coordinates %d, %d\n",temp.x,temp.y); return *this; } 

or for example

Point & Point::operator=( const Point &Y ) { this->x = Y.y; this->y = Y.x; //std::printf("temp has coordinates %d, %d\n",temp.x,temp.y); return *this; } 

As for your statement

When I have the following in main, I don't understand why I don't end up with B.x = 0, B.y = 3.

then you could write using your assignment operator

Point C = B = A; std::printf("C has coordinates %d, %d\n",C.x,C.y); 

That is the returned temporary object created within your assignment operator is used to initialize the newly defined object C by means of the default copy constructor. Again the object B will be unchanged as pointed out above.

One more the above code snippet is equivalent to

Point C = B.operator =( A ); std::printf("C has coordinates %d, %d\n",C.x,C.y); 

Now as you can see the returned temporary object is used to initialize C.

Sign up to request clarification or add additional context in comments.

2 Comments

I know very well that it creates a temporary copy of my object within the assignment operator and that this is unnecessary, but why does it not return to B? What is it returned to? How were you able to conclude it is not used in the program?
@ArtinOstal Reread my answer one more. Your operator does not change the object itself for which it is called. Neither the data member x nor the data member y of the object for which the operator is called is changed within the operator. The object stays unchanged. The result of the operator call is the returned temporary object. I showed you that in the last code example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.