Use of explicit keyword in C++

In this article, we will discuss the explicit keyword in C++ with its example.

The explicit keyword is used with constructors in C++ to prevent them from doing implicit conversions. A C++ explicit constructor is marked to not implicitly convert types. It is important because implicit conversions frequently produce unexpected consequences. If we have a constructor with a single parameter in a class, the constructor transforms the single argument to the class produced and becomes a conversion constructor. We use explicit keywords in C++ to avoid such implicit conversions.

Working of C++ Explicit:

First, let us define constructors in C++. Constructors are unique methods in C++. They are invoked automatically whenever a class object is created. Using constructors, we initialize the new object's data members.

Now, we will look at implicit conversions and how they can be avoided using C++ explicit keywords. It occurs without our directly instructing the compiler. Converting one data type to another is referred to as casting. Without the explicit C++ compiler, the compiler can execute the conversion automatically, removing the requirement for us to perform casting.

We tell the compiler not to execute any implicit conversions on a constructor by including the keyword explicitly before it. Instead of the compiler executing undesirable type operations, we want those constructs to be explicitly invoked.

Examples:

Explicit constructors can be defined in C++ to avoid implicit type conversions. While a constructor is made explicit, the compiler cannot conduct any implicit conversions while initializing an object using that constructor. Here are several C++ examples using explicit constructors:

Example:

Output:

number1: 72
number2: 85

Explanation:

  • In this example, the code defines a class named MyNumber, which encapsulates an integer value (number) and provides an explicit constructor to initialize it.
  • The constructor explicit MyNumber(int value) is declared explicit, meaning it cannot be used for implicit type conversions. It requires an explicit type conversion when constructing MyNumber
  • In the main function:
  • MyNumber number1(72); This line directly initializes number1 with an integer value of 72. It is allowed because it's direct initialization.
  • MyNumber number2 = MyNumber(85); It explicitly calls the constructor with the argument 85 and initializes number2 with the result. It is also allowed due to the explicit constructor call.
  • The lines MyNumber num3 = 55; and MyNumber num4 = 3.14; These lines are commented out because they would result in compilation errors. These lines attempt to perform implicit type conversions, which are not allowed because the constructor is declared explicit. We would need to use explicit type conversion to fix these errors, such as MyNumber num3(static_cast<int>(55)); or MyNumber num4(static_cast<int>(3.14));.
  • Finally, the code prints the values stored in number1 and number2 to the console using std::cout

Uses of explicit keyword:

  1. Preventing Implicit Conversions: When a constructor is designated as explicit, it prevents its parameter from being automatically or implicitly converted to the class type. It helps in the prevention of accidental and potentially error-prone type conversions.
  2. Strict Requirements for Constructors: We can develop constructors that impose specific criteria or constraints on their parameters. Declaring the constructor explicitly requires users to specify the right type, demonstrating that they know the requirements.
  3. Avoiding Ambiguity: In events when numerous constructors could be used for initialization, the explicitly specified keyword can resolve ambiguity. It helps the compiler select the appropriate constructor based on the arguments provided.
  4. Preventing Unintended Object Creation: A class may have a method called a constructor that accepts a single argument, and we wish to prevent instances from being created by accident using this construction.





Latest Courses