To resolve the parameters to a function, the compiler is permitted to do one implicit conversion.
This implies that the compiler can utilise constructors with a single argument to convert from one type to another to find the correct type for a parameter.
Here's an example class with a constructor that can be used for implicit conversions:
class Foo
{
private:
int m_foo;
public:
// single parameter constructor, can be used as an implicit conversion
Foo (int foo) : m_foo (foo) {}
int GetFoo () { return m_foo; }
};
Here's a simple function that takes a Foo object:
void DoBar (Foo foo)
{
int i = foo.GetFoo ();
}
and here's where the DoBar function is called:
int main ()
{
DoBar (42);
}
The parameter is an int rather than a Foo object.
However, Foo has a function Object() { [native code] } that accepts an int, which may be used to transform the parameter to the right type.
The compiler may perform this just once for each argument.
By including the explicit keyword to the function Object() { [native code] }, the compiler is prevented from utilising that function Object() { [native code] } for implicit conversions.
Including it in the preceding class will result in a compiler error at the function call DoBar (42).
DoBar (Foo (42)) is now required to explicitly request conversion.
You might want to do this to avoid unintentional building, which might mask flaws.
Exaggerated example:
You have a MyString class with a function Object() { [native code] } that creates a string of the specified length.
You have a function print(const MyString&) and an overload print (char *string), and you call print(3) (when you meant to call print("3").
You expect it to display "3," but it instead prints an empty string of length 3.