"C + + Primer" mentions:
A constructor that can be called with a single parameter defines an implicit conversion from the formal parameter type to the class type. ”
It should be noted here that "invocation with a single parameter" does not mean that a constructor can have only one parameter, but that it may have more than one parameter, but those parameters have default arguments.
So what is "implicit conversion"? The above statement also says that it is an automatic conversion from a constructor parameter type to a compiler of that class type .
Here's a look at the code:
#include"stdafx.h"#include<string>#include<iostream>using namespacestd;classBook//defines a book class{ Private: string_BOOKISBN;//the ISBN number of the book float_price;//the price of the book Public: //defines a member function, which is the function that "expects an argument to be a class type"//This function is used to compare two books with the same ISBN number BOOLISSAMEISBN (ConstBook &Other ) { returnother._bookisbn==_BOOKISBN; } //The constructor of the class, which is the "constructor that can be called with one parameter" (although it has two parameters, but one of which has a default argument and can be called with only one argument)Book (stringIsbnfloatPrice=0.0f): _BOOKISBN (ISBN), _price (price) {}};intMain () {Book A ("a-a-a"); Book B ("B-b-b"); cout<<A.ISSAMEISBN (B) <<endl;//Do a decent comparison without having to convertcout<<A.ISSAMEISBN (string("a-a-a")) <<endl;//An implicit conversion occurs here: The string type-->book type, which is converted with the constructor of book to satisfy the parameter expectation of the ISSAMEISBN function. COUT<<A.ISSAMEISBN (Book ("a-a-a")) <<endl;//Create temporary objects explicitly, which is what the compiler is doing. System ("Pause");}
As you can see in the code, the ISSAMEISBN function expects a book class type parameter, but we pass a string type to it, which is not what it wants! Fortunately, there is a constructor in the book class that is invoked with a string type argument that the compiler calls to implicitly convert the Stirng type to the book type (constructs a book temporary object ). Passed to the ISSAMEISBN function again.
Implicit class-type conversions can still be risky, as the markup above implicitly transforms the temporary variables of the class and disappears after completion, and we construct an object that is discarded after the test is completed.
We can suppress this conversion by explicit declarations:
Explicit Book (string ISBN,float price=0.0f): _bookisbn (ISBN), _price (price) {}
The explicit keyword can only be used on a constructor declaration inside a class. In this way, the book class constructor cannot be used to implicitly create the object, and the code above will appear with this hint:
users can now only display type conversions and explicitly create temporary objects.
To summarize:
- A call can be made with one argument, not a constructor can have only one parameter.
- Implicit class-type conversions are prone to errors, and unless you have a clear reason to use implicit class-type conversions, the constructors that can be called with one argument are declared as explicit.
- Explicit can only be used for declarations of the inner constructors of a class. While it avoids the problem of implicit type conversions, it requires the user to be able to explicitly create temporary objects (which are required by the user).
Implicit class-Type conversions