Synopsis: | Non-copy-constructors that can be called with one argument shall be declared as explicit |
Language: | C++ |
Severity Level: | 2 |
Category: | Class Interface |
Description: |
When using constructors with single arguments, these constructors need to be declared with the keyword explicit. This keyword prevents implicit type conversions for the arguments to the constructor. explicit CTestClass(SHORT nValue); This syntax prevents an implicit conversion from the float argument in the next example, which can cause undesirable side effects. CTestClass TestClass = 3.2; // Does not work This rule does not apply to copy-constructors, because they do not define type conversions. This rule does apply to constructors with N arguments, with N or N-1 default values. See also [OLC#007] and [CON#001]. |