What is this conversion called?
Is there a name or a term for this type of conversion in the c++ community? Has anyone seen this conversion be referred to as "implicit conversion"?
class ALPHA{};
class BETA{
public:
operator ALPHA(){r开发者_如何学运维eturn alpha;}
private:
ALPHA alpha;
};
void func(ALPHA alpha){}
int main(){
BETA beta;
func(beta);
return 0;
}
It's normally called a conversion function. It isn't an implicit conversion per se, but does allow implicit conversion to the target type.
Edit: just checked to be sure -- §12.3.2 of the standard uses the phrase "conversion function".
Edit2: I checked in the official standard, which isn't (at least supposed to be) freely available (though you can buy it from the ISO or most member standards organizations such as ANSI, BSI, etc.) A lot of people prefer to save the money and use the final committee draft, which is free.
That is commonly called a conversion operator.
Implicit conversion is often referred to as coercion.
When calling func(beta)
object beta
is implicitly converted to type ALPHA
. This is only possible because you have implemented operator ALPHA()
.
精彩评论