In C + +, implicit conversions mainly involve the following scenarios:
1) Parameter Object transfer
At this point, C + + needs to convert the object to a reference pass through a temporary variable, such as:
Func (a A); Func (a);//conversion is as follows: a TMP (a); func (TMP);
2) return value Pass
&TMP);
3) Type implicit conversions, as in this case:
A:A (int2; call a constructor or transform function to convert and then assign a value.
The comprehensive test procedure is as follows:
/** Test C + + implicit conversions*/#include<iostream>using namespacestd;classA { Public: A (): M (0) {cout <<"a::a ()"<<Endl;} A (inti): M (i) {cout <<"a::a (int)"<<Endl;} A (Consta& a) {m = a.m.; Cout <<"A (a a)"<<Endl;} ~a () {cout <<"~a::a ()"<<Endl;} Aoperator+(inti) {returnA This->m +i); } Aoperator=(ConstA &a) {cout<<"operator ="<<Endl; A b=A; returnb; }Private: intm;};classB { Public: B () {cout<<"b::b ()"<<Endl;} ~b () {cout <<"~b::b ()"<<Endl;}};/** * There are 3 cases of implicit conversion: * 1. Return value and formal parameter * 2. Operator overloading * 3. Type conversions*/a func1 (a a) {cout<<"func1"<<Endl; returnA;}voidFunc2 () {cout<<"Func2"<<Endl;}intMain () {a A; A b; cout<<"func1:"<<Endl; Func1 (a); cout<<"func1 (2):"<<Endl; Func1 (2); cout<<"A + 2:"<<Endl; A+2; cout<<"end."<<Endl; return 0;}
C + + implicit conversions