It is nothing but function object which can be treated as function using operator (). C++ allows operator() overloading. Functor object class overloads/defines its own operator () which allows the class object to be called as function itself.

Example

  • Functor creation and operator() overloading
class sum {
    private:
        int num;
    public:
        sum(int n): num(n) {}
        //Operator overloading 
        int operator() (int n) const {
           return n + 10;
        }
};

int main(){
   int number = 10;
   sum obj(number);
   int new_num = obj(10);
   cout << new_num << endl; 
   return 0;
}

Leave a comment