Message from C, C++ discussions
December 2019
— /warn Read the rules: no screen photo.
User Basel has 1/2 warnings; be careful!
The latest warn was because:
Read the rules: no screen photo.
— Read about lvalue& type.
Method returns a reference type of Point class.
— That means that you need to use normal language instead of c++
— Returning a reference in such cases is a good way to UB.
LikePoint& a = Point(10, 20).setX(15);
Or in more sophisticated case:Point& a = (b + c).setX(15);
OrPoint& a = functionThatReturnsPoint().setX();
— No, storing a reference on the stack is the problem here
— Just, unless you get a reference of guaranteed existence, never store a reference
— Someone can see that it's guaranteed in a specific case, then someone else can change the code on the right side so it starts pointing to a temporary.
— I don't see a point of returning a reference here, because 2d point is usually a pod object, so it's copy is cheap
— I see your point, but i don't see totally crazy. Actually, operator= is more or less the same and you always return a reference
— Hello everyone I'm vaibhav aka Tommy
— 👋