首页 > 其他 > 详细

隐式转换构造函数、仿函数、转换函数实例

时间:2017-03-13 16:14:46      阅读:333      评论:0      收藏:0      [点我收藏+]
class B {
public:
    //隐式转换
    B(int i)
    {
        cout << i << endl;
        data = i;
    }
    //仿函数
    bool operator() (int i)
    {
        cout << i << endl;
        return i > 0;
    }
    //转换函数
    operator string()
    {
        return "class_string";
    }
private:
    int data;
};

int main() {
    B inst = 100;
    bool temp = inst(-100);
    if (temp) {
        cout << "hello operator" << endl;
    }

    string str = inst;
    cout << str << endl;

    getchar();
    return 0;
}

 

隐式转换构造函数、仿函数、转换函数实例

原文:http://www.cnblogs.com/kaishan1990/p/6542410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!