#include "iostream"
using namespace std;
class Cgirl
{
public:
int m_bianhao;
string m_name;
double m_weight;
//默认构造函数
Cgirl(){m_bianhao=8 ;m_name="西施";m_weight=50.1;}
explicit operator int(){return m_bianhao;}
operator string(){return m_name;}
operator double(){return m_weight;}
};
int main(){
Cgirl g1;
int a=int(g1);
cout<<"a="<<a<<endl;
string b=string(g1);
cout<<"b="<<b<<endl;
double c=double(g1);
cout<<"c="<<c<<endl;
short d=(int)g1;//显式转换
//编译器发现了,int类型不可以用,自动用double来转换。
int e=g1.operator int();
cout<<"e="<<e<<endl;
}
输出:
a=8
b=西施
c=50.1
e=8
发表回复