Next: High Integrity CPP Guideline 3.1.12  Up: 3.1 General  Previous: High Integrity CPP Rule 3.1.10  Contents

High Integrity CPP Rule 3.1.11   Do not provide conversion operators for class types.
(QA C++  2181)

Justification

Conversion operators should not be used because implicit conversions using conversion operators can take place without the programmers knowledge. Conversion operators can lead to ambiguity if both a conversion operator and a constructor exist for that class. In most cases it is better to rely on class constructors.

   
   class C;
   
   class D 
   {
   public:
      D( C );          // 1
   };
   
   class C 
   {
   public:
      operator D();    // 2
   };
   
   void foo( D );
   
   void bar() 
   {
      C c;
      foo( c );        // ambiguous (convert to D by 1 or 2?)
   }
See also

Rule 3.1.10

Reference

Effective C++ Items 18, 27;More Effective C++ Item 5;Industrial Strength C++ 7.19;


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