zoukankan      html  css  js  c++  java
  • Use of explicit keyword in C++

      

      Predict the output of following C++ program.

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Complex
     6 {
     7 private:
     8     double real;
     9     double imag;
    10     
    11 public:
    12     // Default constructor
    13     Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) 
    14     {
    15     }
    16     
    17     // A method to compare two Complex numbers
    18     bool operator == (Complex rhs) 
    19     {
    20         return (real == rhs.real && imag == rhs.imag)? true : false;
    21     }
    22 };
    23 
    24 int main()
    25 {
    26     // a Complex object
    27     Complex com1(3.0, 0.0);
    28     
    29     if (com1 == 3.0)
    30         cout << "Same";
    31     else
    32         cout << "Not Same";
    33     return 0;
    34 }

      Output: The program compiles fine and produces following output.

      Same
      

      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.

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Complex
     5 {
     6 private:
     7     double real;
     8     double imag;
     9     
    10 public:
    11     // Default constructor
    12     explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) 
    13     {
    14     }
    15     
    16     // A method to compare two Complex numbers
    17     bool operator== (Complex rhs) 
    18     {
    19         return (real == rhs.real && imag == rhs.imag)? true : false;
    20     }
    21 };
    22 
    23 int main()
    24 {
    25     // a Complex object
    26     Complex com1(3.0, 0.0);
    27     
    28     if (com1 == 3.0)
    29         cout << "Same";
    30     else
    31         cout << "Not Same";
    32     return 0;
    33 }

      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.

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Complex
     5 {
     6 private:
     7     double real;
     8     double imag;
     9     
    10 public:
    11     // Default constructor
    12     explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) 
    13     {
    14     }
    15     
    16     // A method to compare two Complex numbers
    17     bool operator== (Complex rhs) 
    18     {
    19         return (real == rhs.real && imag == rhs.imag)? true : false;
    20     }
    21 };
    22 
    23 int main()
    24 {
    25     // a Complex object
    26     Complex com1(3.0, 0.0);
    27     
    28     if (com1 == Complex(3.0))
    29         cout << "Same";
    30     else
    31         cout << "Not Same";
    32     return 0;
    33 }

      Output: The program compiles fine and produces following output.

      Same

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

      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-26  21:41:25

  • 相关阅读:
    JSOI2010 满汉全席
    LNOI2014 LCA
    BZOJ3689 异或之
    Codeforces Round #553 div.2
    AHOI2013 差异
    SDOI2016 生成魔咒
    NOI2006 最大获利
    没有过的题QAQ
    NOI2014 动物园
    HDU4622 Reincarnation
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3444347.html
Copyright © 2011-2022 走看看