#include <iostream>
using namespace std;
class length
{
int meter;
public:
length(int m)
{
meter=m;
}
operator double()
{
return (1.0*meter/1000);
}
};
int main()
{
length a(1500);
double m=double(a);
double n=float(a);
double l=int(a);
cout <<"m="<<m<<"km"<<endl;
cout <<"m="<<n<<"km"<<endl;
cout <<"m="<<l<<"km"<<endl;
return 0;
}
结果如下:
m=1.5km
m=1.5km
m=1km
为什么只float(a)
和定义一起工作?int(a)
double(Class length)