C++ sınıf yapılarında constructor tanımları, normal olarak implicit yapıdadır. Implicit yapıdaki constructor derleme zamanında conversion yapabilir. Bu durumu engellemek için constructor’ı explicit yapıda tanımlarız. Bu tanımla beraber explicit yapıdaki constructor, implicit conversion yapılmasını engeller.
Bu durumu kod ile açıklayacak olursak,
#include <iostream>
class Holder
{
private:
int mNum;
public:
explicit Holder(int num)
: mNum(num)
{}
int getNumber() { return mNum; }
};
void showNumber(Holder h)
{
int i = h.getNumber();
std::cout << i << std::endl;
}
int main(int argc, const char** argv)
{
/**
* If we can define implicit constructor, we can send 42 directly
* But we defined explicit constructor that's why we can not send 42,
* Firstly we must init class with constructor after that we call showNumber function.
*
* error: could not convert ‘42’ from ‘int’ to ‘Holder’
**/
showNumber(42);
Holder h = Holder(59);
showNumber(h);
return 0;
}
Kodumuzu inceleyecek olursak, constructor explicit tanımlandığı için showNumber(42) satırının hata verdiğini görebilirsiniz. Eğer constructor explicit tanımlanmasaydı derleyici showNumber(42) satırını conversion yaparak sorunsuz bir şekilde çalışacaktı.