0

Is there any reason to check for self assignment, if you're not using array's or pointers, when overloading the = operator?

For example, if the only datatypes in Vehicle are int cylinders and string manufacturer, do I really need to check before assigning? If so, what's the point?

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by vijayan121
0

> if the only datatypes in Vehicle are int cylinders and string manufacturer, do I really need to check before assigning?
you really do not need to write an assignment operator at all; the compiler will synthesize a (non-trivial) assignment operator with the correct semantics.

> If so, what's the point?
only efficiency (avoid the call to string's operator=, which would check for a self assignment in any case).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.