Search code examples
functionobjectfunctor

A function object:


A function object:

  1. is an instance of a class for which operator () is a member function.
  2. is an instance of a class for which operator --> is a member function.
  3. is a pointer to any function
  4. is a member function of a class

My attempt:

A Function Object, or Functor (the two terms are synonymous) is simply any object that can be called as if it is a function. An ordinary function is a function object, and so is a function pointer; more generally, so is an object of a class that defines operator().

Functors (Function Objects or Functionals) are simply put object + ().

It seems to me that option (3) is true, but I'm confused with option (1), is it true?

Can you explain it, please?


Solution

  • A function object is an instance of a class that defines the parenthesis operator as a member function. When a function object is used as a function, the parenthesis operator is invoked whenever the function is called. Consider the following class definition:

    class biggerThanThree 
     {
       public:
       bool operator () (int val)
       { return val > 3; }
    };
    

    If we create an instance of class biggerThanThree, every time we reference this object using the function call syntax, the parenthesis operator member function is invoked.


    Reference:

    1. http://www.cplusplus.com/reference/functional/binary_function/
    2. https://msdn.microsoft.com/en-in/library/aa985932.aspx