Updated April 17, 2023
Introduction to C++ explicit
In C++, explicit is a keyword used before constructors and is defined as making the constructor not conduct any implicit conversion by specifying the keyword explicit. This is defined to avoid few implicit conversions when a class contains a single argument constructor, which usually the compiler considers it as a conversion constructor or implicit conversion, which may, in turn, lead to some unwanted outputs and to avoid such results, we have to define such constructors with an explicit keyword before the constructor name. In general, the explicit constructor is declared where we cannot use implicit constructors, and such explicit constructors always have single or more arguments.
Working of explicit Keyword in C++ with Examples
In this article, we will see the explicit keyword used for constructor where we cannot use an implicit constructor. In C++, we know that constructors are mainly used for initializing or creating objects of the class, which are by default provided by the compiler, and we can use such constructors for type conversion like implicit or explicit conversions with the constructors that are within the class declaration. This explicit keyword is optional, which takes only a single argument always, and such constructors can be used in typecasting.
If the implicit conversions are uncontrollable, then we need to specify such constructor with a keyword explicit declaration for the constructors within the class declaration, but we should note one thing we cannot use this explicit keyword for the default constructor. In general, we can say that specifying the explicit keyword as a function specifier to a constructor with a single or more argument within the class declaration is used for preventing the class from unwanted type conversions; else, without this explicit keyword, it would be a converting constructor.
Let us consider the below example to why and where to use explicit keyword in C++:
Example #1
Code:
#include <iostream>
using namespace std;
class Edu{
char var_c;
public:
Edu() {}
Edu(char c): var_c(c) {};
void print() {std::cout << "The value is "<< var_c <<endl;}
};
void disp(Edu u)
{
u.print();
}
int main()
{
Edu ins('E');
Edu ins1 = 'D';
cout<<"Below value is obtained when constructor is called normally :" << endl;
disp(ins);
cout<<"\n" <<endl;
cout<<"Below value is obtained when compiler implicitly is called to convert it to char type from Edu type is : " << endl;
disp(ins1);
return 0;
}
Output:
In the above program, we can see firstly we have declared a class “Edu” where we have declared a character type variable var_c and we created a default constructor with zero arguments and another constructor with class name with single argument with variable c of char type and we are printing the value that is passed to this function and we have to remember that we have to pass value of char type only but we can see in main() function we created first class Edu object “ins” where we are passing “E” alphabet to disp() function which has one argument of “Edu” type so it will print the value as it calls the constructor normally but when we created another object “ins1” of class Edu and where disp() function we are passing with the class type “Edu” argument so when we are calling disp() function now and trying to assign “D” alphabet which is char type to this ins1 object which is “Edu” type so here the compiler will implicitly convert this “Edu” type variable to char type variable and it gives the output with the char value as shown in above screenshot which is not correct sometimes so to avoid such implicit calls we have to add “explicit” keyword to the constructor with single argument such as “explicit Edu(char c): var_c(c) {};” So when we change this above code we get error as shown in below screenshot.
So here is the output; we can see that conversion from char type to Edu is non-scalar, which is not a legal call, so it’s a good practice to use explicit keyword in such case or wherever there is a chance implicit constructor conversion. So in the above code, we have written the keyword “explicit” for the “Edu” constructor with a single argument of char type.
Now let us see an example with two arguments; we will see if the explicit works with a two-argument constructor.
Example #2
Code:
#include <iostream>
using namespace std;
class Edu
{
private:
int i;
int j;
public:
Edu(int a = 0, int b = 0) : i(a), j(b) {}
bool operator== (Edu e) {
return (i == e.i && j == e.j)? false : true;
}
};
int main()
{
Edu ins(3, 4);
cout << "It will print matched if the given values match else not same which uses implicit call to convert int to Edu type:" <<endl;
cout<< "\n" <<endl;
if (ins == 3)
cout << "matched";
else
cout << "No match";
return 0;
}
Output:
In the above program, we can see this is also the same code as above, but we are using two arguments in the constructor. It is usually a best practice to apply or use explicit when there is a single argument constructor because two or more argument constructor may lead to confusion in calling them in the main function. So in the above example, it works as it again, the constructor is called implicitly by the compiler where it is not the legal or correct form of getting outputs. So to avoid such undesirable execution of programs that may yield outputs, logically, it’s not correct for converting the types, so we use the explicit keyword for the constructor. In the above code also we can rewrite the code with an explicit keyword before declaring the constructor within the class, such as “explicit Edu(int a = 0, int b = 0): i(a), j(b) {}” so the output will look like below screenshot.
Conclusion
In this article, we can conclude that in C++, the explicit keyword is usually used for the constructors used to create class objects within the class declaration. In this article, we saw the explicit keyword is used only for constructors when we want to avoid the implicit calls made by the compilers, which usually make the constructors as converting constructors which converts variables from one type to another type which is not correct or legal to do so. It’s the best practice to always use explicit keyword in such cases where it will give an error saying there type non-scalar type; we have seen examples in the above article.
Recommended Articles
This is a guide to C++ explicit. Here we discuss the introduction and working of explicit keyword in C++ with examples, respectively. You may also have a look at the following articles to learn more –