zoukankan      html  css  js  c++  java
  • 隐式转换构造函数、仿函数、转换函数实例

    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;
    }

      类型转换函数

      operator 目标类型()

      {

        ...

        return 目标类型的数据;

      }

  • 相关阅读:
    每日日报46
    每日日报45
    每日日报44
    每日日报43
    每日日报42
    每日日报41
    每日日报40
    每日日报之一周总结
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/kaishan1990/p/6542410.html
Copyright © 2011-2022 走看看