C++ Conversion Function (1)

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 (江東敏)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,105評論 0 23
  • 你和不一樣的人在一起,就會有不一樣的人生。 在現(xiàn)實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的...
    三仙居士閱讀 731評論 2 3
  • 《道德經(jīng)》第六十三章 為大于細 為無為,事無事,味無味。大小多少,報怨以德。圖難于其易,為大與其細;天下難事,必作...
    日就月將閱讀 587評論 0 0
  • 最近在讀范冰的《增長黑客》,忽然想將互聯(lián)網(wǎng)思維應用到現(xiàn)實生活中。然后,我發(fā)現(xiàn)生活處處都是坑,思維總是不夠用。理論即...
    簫念念閱讀 1,608評論 5 9
  • 干凈的診室內(nèi),方威一絲不茍地做著工作日志記錄,同時喊著:“下一位。”這時一位年輕的農(nóng)村母親拉著七八歲的孩子走了進來...
    Jim_Liu閱讀 197評論 0 0

友情鏈接更多精彩內(nèi)容