The explicit Dialog(QWidget *parent = 0) constructor contains the keyword explicit, which is used to modify the constructor. In the past under Windows to write programs, basically did not encounter this keyword, so this keyword is to do what?

The keyword explicit disallows “single-argument constructors” from being used for automatic type conversions. It is mainly used for “embellishing” constructors. Specify that constructors are display-only to prevent unnecessary implicit conversions.

Just look at this sentence seems not easy to understand, let’s take a simple example.

          //main.cpp

#include <iostream>

            using namespace std;

            class Test

{

public:

Test(int a)

{

m_data = a;

}

                 void show()

{

cout << “m_data = ” << m_data << endl;

}

            private:

int m_data;

};

void main(void) { Test t = 2; // Assigns a constant to an object. How (); }

M_data = 2.

Why is that? It turns out that C++ implicitly constructed a temporary object, Test(2), and assigned it to t(the default constructor is called here, not the overloaded “=” because this is when the object is created). So, if you add the keyword explicit to the constructor, the constructor becomes explicit Test(int a). Compile again and the compiler will report an error. At this point, you can only use the constructor explicitly: Test t = Test(2).