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

  • 相关阅读:
    UVA 10618 Tango Tango Insurrection
    UVA 10118 Free Candies
    HDU 1024 Max Sum Plus Plus
    POJ 1984 Navigation Nightmare
    CODEVS 3546 矩阵链乘法
    UVA 1625 Color Length
    UVA 1347 Tour
    UVA 437 The Tower of Babylon
    UVA 1622 Robot
    UVA127-"Accordian" Patience(模拟)
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3444347.html
Copyright © 2011-2022 走看看