Explicit conversion constructors (C++ only)

The explicit function specifier controls unwanted implicit type conversions. It can only be used in declarations of constructors within a class declaration. For example, except for the default constructor, the constructors in the following class are conversion constructors.

class A
{  public:
   A();
   A(int);
   A(const char*, int = 0);
};

The following declarations are legal.

A c = 1;
A d = "Venditti";

The first declaration is equivalent to A c = A(1).

If you declare the constructor of the class with the explicit keyword, the previous declarations would be illegal.

For example, if you declare the class as:

class A
{  public:
   explicit A();
   explicit A(int);
   explicit A(const char*, int = 0);
};

You can only assign values that match the values of the class type.

For example, the following statements are legal:

A a1;
A a2 = A(1);
A a3(1);
A a4 = A("Venditti");
A* p = new A(1);
A a5 = (A)1;
A a6 = static_cast<A>(1);