美文网首页收集一些技术好文
C++ Conversion Function (1)

C++ Conversion Function (1)

作者: JoeJiang | 来源:发表于2017-07-23 12:25 被阅读102次

    Copyright @ Joel Jiang(江东敏) at 2017.07.30 23:00 p.m
    in Shenzhen.China. LIST- TE- E11 -04

    Conversion function (conversion function) is a special type of class member function. It defines a user-defined transformation to convert a class object into some other type.In the class declaration, we can declare the transformation function by specifying the key word operator and then adding the target type of the transformation.
    Format model: A -> B


    <一>、Have a conversion function in class:
    You can easily reform a specifies type of data into a object inside the class..But on the contrary it' s difficult, which change a object inside a class into a specifies type of data. For example, converting a Complex class object to a double type data. Fortunately,C++ supply the type conversion function as a complement of dealing this question. If you have claimed a "complex“ class, you can define the type conversion function in the complex class as follow:

    operator double( ) {       
            return real;    
     }
    
    class Fraction{
           public:
    Fraction(int num,int len = 1):m_nummer,m_deno(len){}
    operator double()const{
              return(double)(m_nummer/m_deno);
    }
    
    private:
    int m_nummer;
    int m_deno;
    };
    
    Fraction f(3,5);
    double d = 4+f;
    

    Call the operater double function to change the f - >double.

    <二>、No have a explict key and only have one argument in class:
    In this case, we look at the code as follow :

    class Fraction{
    public:
    Fraction(int num,int len = 1):m_nummer,m_deno(len){}
    Fraction operator +(const Fraction&f){
    return Fraction(...);
    }
    
    private:
    int m_nummer;
    int m_deno;
    };
    
    Fraction f(3,5);
    double d = 4+f;
    

    Note: First, it may call the "Fraction(int num,int len = 1)" to change to "4 ->Fraction (4,1),then call the "operator+".

    <三>、Have a conversion function and have no explicit key,but with one argument ctor:

    class Fraction{
    public:
    Fraction(int num,int len = 1):m_nummer,m_deno(len){}
    operator double()const{  
    return(double)(m_nummer/m_deno);
    Fraction operator +(const Fraction&f){
    return Fraction(...);
    }
    
    private:
    int m_nummer;
    int m_deno;
    };
    
    Fraction f(3,5);
    
    double d2 = 4+f;//
    

    Conclusion : [error]ambiguous !!!

    <四>、Have a key of explicit and have one argument ctor:
    Less use the key of explict in C++, it is mainly used in some places that the constructor function, explicit is means that prevent the occurrence of an implicit conversion that should not be allowed by the transformation constructor ,the code as follow:

    class Fraction{
    public:
    
    explict Fraction(int num,int len = 1):m_nummer,m_deno(len){} 
    
    operator double()const{
    return(double)(m_nummer/m_deno);
    
    Fraction operator +(const Fraction&f){
    return Fraction(...);
    }
    
    private:
    int m_nummer;
    int m_deno;
    };
    
    Fraction f(3,5);
    
    double d2 = 4+f;//  
    

    Conclusion: [error]conversion from 'double' to 'fraction' requestion, cant change the 4->4/1.


    Reference:
    http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/language/ref/cplr385.htm
    http://en.cppreference.com/w/cpp/language/cast_operator
    https://stackoverflow.com/questions/2171799/conversion-operator-as-standalone-function?rq=1


    The sharing of knowledge, the spirit of encouragement.
    By Joel Jiang (江东敏)

    相关文章

      网友评论

        本文标题:C++ Conversion Function (1)

        本文链接:https://www.haomeiwen.com/subject/ywtmkxtx.html