| High Integrity CPP Rule 3.5.4 | Make binary operators non-members to allow implicit conversions of the left hand operand. |
| (QA C++ 2070) |
| Justification |
By making binary operators members, a conversion to the left hand side of the binary operator is not possible.
class complex
{
public:
complex( float r, float i = 0 );
complex operator+( const complex& rhs );
};
void Add()
{
complex a( 1, 0 );
a = a + 2; // fine: 2 is converted to complex
a = 2 + a; // error: no applicable operator +
}
|
| Reference |
Effective C++ Item 19; |