[英]Automatic type conversion function call C++
Is there a way to automatically convert to a certain type: 有没有一种方法可以自动转换为特定类型:
class A
{ public:
int f();
float g();
};
r(int x){}
t(float x){}
...
r(A) //automagically call r(A.f())
t(A) //automagically call t(A,t())
sort of like when toString is called for Java objects when you print them. 类似于在打印Java对象时调用toString时。
Yes, you can provide cast operators for A
. 是的,您可以为A
提供强制转换运算符 。
class A
{
public:
int f();
float g();
operator int() { return f(); }
operator float() { return g(); }
};
But you then call r
and t
on an instance: 但是,然后在实例上调用r
和t
:
A a;
r(a);
t(a);
Here's the full code: http://ideone.com/Pfa4v 这是完整的代码: http : //ideone.com/Pfa4v
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.