c++ 中函數操作符operator() 的使用

函數操作符()可以實現將對象當函數來使用

#include <iostream>

using namespace std;

class Square{
    public:
        double operator()(double x)const{
            return x * x;
        }
        int operator()(int a,int b,int c = 9){
            return a + b - c;
        }

};
int main(void){
    Square square;
    cout << square(12.) << endl;        //144
    cout << square(10, 20, 30) << endl; //0
    cout << square(10, 20) << endl;     // 21
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章