conversion function-- conversion function

Type conversion functions with explicit keywords

1. The type conversion function

In C++may be specified using a constructor to a type of data into an object class, type conversion functions may be used (type conversion function) converting a class object for other types of data. example:

Fraction (Fraction) class

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Fraction
 6 {
 7 public:
 8     Fraction(int num, int den = 1) :m_numberator(num), m_denominator(den) {}
 9 
10     operator double() const //转换函数
11     {
12         return (double)m_numberator / m_denominator;
13     }
14 
15 private:
 16      int m_numberator;   // molecule 
. 17      int m_denominator; // denominator 
18 is  };
 . 19  
20 is  int main ()
 21 is  {
 22 is      Fraction F ( . 3 , . 4 );
 23 is      Double D = . 3 + F;    // call opertor double () f into the Double 
24      COUT D << << endl;
 25      return  0 ;
 26 is }

Fraction f is a variable of type 3 and thus can not be directly added, then the compiler to find the class scores, found operator double function, which can be converted to a floating-point type object type variable. So this is what we call the function type conversion functions (type conversion function).

Converting the general form of a function:

. 1  operator type name () const 
2  {
 . 3          // achieve conversion       
4 }
  1. You must be a member function
  2. No return type
  3. Parameter list is empty
  4. Preferably const

2.explicit keyword

Overload can also +be doubleconverted to Fraction type class type. Thus the class code as follows:

 1 #include <iostream>
 2 
 3 class Fraction
 4 {
 5 public:
 6     Fraction(int num, int den = 1) : m_numberator(num), m_denominator(den) {}
 7 
 8     Fraction operator+(const Fraction &f)
 9     {
10         return Fraction(this->m_numberator + f.m_numberator, this->m_denominator + f.m_denominator);
11     }
12 
13 private:
 14      int m_numberator;   // molecule 
15      int m_denominator; // denominator 
16  };
 . 17  
18 is  int main ()
 . 19  {
 20 is      Fraction F ( . 3 , . 4 );
 21 is      Fraction = D + F . 4 ; // call the non-explicit- the one-argument ctor 4 into Fraction 
22 is      return  0 ;
 23 is }

But if there is type conversion functions at the same time, there is an ambiguity. example:

 1 #include <iostream>
 2 
 3 class Fraction
 4 {
 5 public:
 6     Fraction(int num, int den = 1) : m_numberator(num), m_denominator(den) {}
 7     
 8     operator double() const
 9     {
10         return (double)m_numberator / m_denominator;
11     }
12 
13     Fraction operator+(const Fraction &f)
14     {
15         return Fraction(this->m_numberator + f.m_numberator, this->m_denominator + f.m_denominator);
16     }
17 
18 private:
19     int m_numberator;  //分子
20     int m_denominator; //分母
21 };
22 
23 int main()
24 {
25     Fraction f(3, 4);
26     double d = f + 4; //二义性
27     return 0;
28 }
  • fObject types can be type conversion function to convert the class doubletype, then add 4assigned tod
  • 4It may also be converted to the constructor Fractiontype, and then adding the two class types, they can be converted to type conversion functions by doubletype

The solution is to use limit explicit keywords to use, there are two:

  • Keyword before adding explicit direct constructor, to prevent double variable implicitly converted to class types.
  • Keyword before adding explicit conversion operator constructor double, double type represents the function call when the class type conversion only shows, for example: double d = static_cast <double> (f) + 4.

 

Guess you like

Origin www.cnblogs.com/vlyf/p/11688030.html