Explicit is used to modify the class constructor, indicating that the constructor is explicit,
It is a default constructor used for user-defined constructor and cannot be used to convert constructor. Because there are three constructor types, 1 copy constructor 2 convert constructor 3 Constructor
When a class has multiple constructors, the constructor modified by explicit is the default one.
Class string {
PRIVATE:
Int size;
Int capacity;
Char * Buff;
Public:
String ();
String (INT size );
String (const char *);
~ String ();
};
Int main (){
String S = "hello ";
Int NS = 0;
S = 1;
}
S = 1. I originally thought NS = 1, but I wrote an error and wrote it as S = 1, because there is a constructor that accepts an int type parameter, so we will try to use string (1)
To avoid similar errors, we add the explicit keyword to the front,
Explicit string (INT size)
A good example:
Class People {
Public:
Int age;
Explicit people (int ){
Age =;
}
};
Void Foo (void ){
People p1 (10); // method 1
People * p_p2 = new people (10 );
People P3 = 10; // method 3. An error is returned. If explicit it is removed, no error is returned.
}
Gcc-s people. cpp