c++ 重载类型转换运算符
1重载类型转换运算符.cpp
#include<iostream>
using namespace std;
class A{
public:
int m=0;
A()=default;
A(int k){m=k;}
~A(){}
operator int()
{
return m;
}
operator double()
{
return m;
}
};
int main()
{
// A a(100); //直接初始化
A a =100; //拷贝初始化
cout<<a.m<<endl;
//ok: int n = (int)a +100;
// int n = a +10.11;
int n = a +100;
cout<<n<<endl;
}