#include <iostream>
using namespace std;
class stu
{
public:
int a;
double b;
stu()
{
a = 12;
b = 12.345;
}
operator int() const//1、无返回值类型,有返回值。2、无参数。3、必须定义为类成员函数。4、不应该改变对象的内容,所以是coust函数。5、避免过度使用
{
return a;
}
operator double() const
{
return b;
}
};
int main()
{
//float a = 12.123;
stu db;
cout<<(int)db<<endl;
cout<<(double)db<<endl;
system("pause");
return 0;
}
C++ 重载类型转换
于 2022-07-13 09:40:22 首次发布