Next: High Integrity CPP Guideline 3.5.5  Up: 3.5 Operator Overloading  Previous: High Integrity CPP Rule 3.5.3  Contents

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;


HICPP VERSION 2.4  http://www.codingstandard.com   Copyright: © 2007 THE PROGRAMMING RESEARCH GROUP