The following code works fine.
class A
{
private:
int _value;
public:
class AProxy
{
public:
AProxy(int num):_num(num){}
int _num;
};
A(AProxy x):_value(x._num){}
int getvalue(){
return _value;
}
};
void func(A a)
{
cout<<"A is initialized with "<<a.getvalue()<<endl;
}
int main()
{
A a(10);
return 0;
}
a(10) gets converted to a(Aproxy(10))
However, the following code do not work.
class A
{
private:
int _value;
public:
class AProxy
{
class AAProxy
{
public:
AAProxy(int num):_aanum(num){}
int _aanum;
};
public:
AProxy(AAProxy aa):_num(aa._aanum){}
int _num;
};
A(AProxy x):_value(x._num){}
int getvalue(){
return _value;
}
};
void func(A a)
{
cout<<"A is initialized with "<<a.getvalue()<<endl;
}
int main()
{
A a(10);
return 0;
}
a(10) -> a(Aproxy(AAproxy(10))). So the implicit conversion from int to a user-defined type, happens only once ?
It only looks for direct conversions from type A (what it is) to type B (what it should be). There are way too many ways to convert from A through C to B if it were to do that and the problem becomes unbounded; not to mention that ambiguities are quick to come up that way.
Per the C++ standard, the language will only try to resolve user type disconnects with 1 level of implicit conversion. > 1 level requires explicit conversion.
您将获得最多一个隐式用户定义的转换。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.