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?

Recommended Answers

All 2 Replies

> 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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.