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

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,895评论 0赞 23
  • 你和不一样的人在一起,就会有不一样的人生。 在现实生活中,你和谁在一起的确很重要,甚至能改变你的成长轨迹,决定你的...
    三仙居士阅读 745评论 2赞 3
  • 《道德经》第六十三章 为大于细 为无为,事无事,味无味。大小多少,报怨以德。图难于其易,为大与其细;天下难事,必作...
    日就月将阅读 640评论 0赞 0
  • 最近在读范冰的《增长黑客》,忽然想将互联网思维应用到现实生活中。然后,我发现生活处处都是坑,思维总是不够用。理论即...
    箫念念阅读 1,642评论 5赞 9
  • 干净的诊室内,方威一丝不苟地做着工作日志记录,同时喊着:“下一位。”这时一位年轻的农村母亲拉着七八岁的孩子走了进来...
    Jim_Liu阅读 208评论 0赞 0

友情链接更多精彩内容