C++ Conversion Function (1)
3351 ワード
Copyright@Joel Jiang(江東敏)at 2017.07.30 23:00 p.m in Shenzhen.China.LIST-TE-E 11-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:
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 :
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:
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:
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(江東敏)
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(江東敏)