An explicit constructor is one you call. An implicit constructor is one the compiler calls.
Unless you say so all constructor calls are implicit.
You use the explicit keyword to tell the compiler that a cnstructor is off limits:
- class MyClass
-
{
-
public:
-
explicit MyClass(int);
-
};
-
-
int main()
-
{
-
MyClass obj(5); //OK. You are caling the constructor
-
}
-
But let's say you have this function:
- void AFunction(MyClass arg)
-
{
-
//etc..
-
}
and someone calls ths function using an integer by mistake:
Now there is an implicit call to MyClass::MyClass(int) to initialize a MyClass object created from the int. However, if the MyClass constructor is explicit then the compiler will not make the call and you will get a "no suitable constructor" error during the compile.