Use of explicit keyword in C++

Predict the output of following C++ program.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
class Complex
{
private:
    double real;
    double imag;
  
public:
    // Default constructor
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
  
    // A method to compare two Complex numbers
    bool operator == (Complex rhs) {
       return (real == rhs.real && imag == rhs.imag)? true : false;
    }
};
  
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
  
    if (com1 == 3.0)
       cout << "Same";
    else
       cout << "Not Same";
     return 0;
}

chevron_right


Output: The program compiles fine and produces following output.

Same 

As discussed in this GFact, in C++, if a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows conversion of the single argument to the class being constructed.
We can avoid such implicit conversions as these may lead to unexpected results. We can make the constructor explicit with the help of explicit keyword. For example, if we try the following program that uses explicit keyword with constructor, we get compilation error.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
class Complex
{
private:
    double real;
    double imag;
  
public:
    // Default constructor
    explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
  
    // A method to compare two Complex numbers
    bool operator== (Complex rhs) {
       return (real == rhs.real && imag == rhs.imag)? true : false;
    }
};
  
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
  
    if (com1 == 3.0)
       cout << "Same";
    else
       cout << "Not Same";
     return 0;
}

chevron_right


Output: Compiler Error

no match for 'operator==' in 'com1 == 3.0e+0'

We can still typecast the double values to Complex, but now we have to explicitly typecast it. For example, the following program works fine.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
class Complex
{
private:
    double real;
    double imag;
  
public:
    // Default constructor
    explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
  
    // A method to compare two Complex numbers
    bool operator== (Complex rhs) {
       return (real == rhs.real && imag == rhs.imag)? true : false;
    }
};
  
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
  
    if (com1 == (Complex)3.0)
       cout << "Same";
    else
       cout << "Not Same";
     return 0;
}

chevron_right


Output: The program compiles fine and produces following output.

Same 

Also see Advanced C++ | Conversion Operators

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


13


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.