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;

}
posted @ 2016-03-30 13:49  夜色下的港湾  Views(132)  Comments(0Edit  收藏  举报