Published Feb 27, 2023
Contribute to Docs

A functor is an object or structure that can be called like a function by overloading the function call operator (). Public access must be granted to the overloading of the () operator in order to be used as intended. Functors can simplify tasks and improve efficiency in many cases.


For an object to be a functor, the class body must contain the following:

class MyClass {
    type operator()(...) {
      // Function body
  • type is the data type to be returned
  • operator() overloads the function call operator () and (...) are the arguments required to execute the function body.

To use the functor, an instance of the class is created. Then, it is called with arguments passed in:

MyClass myclass;



The following code overloads the function call operator () of class Hello in order to print Hello world! as many times as indicated by the argument (int times) allowing to create functors from such class:

using namespace std;
class Hello {
void operator()(int times){
for (int i = 1; i <= times; i++)
cout << "Hello world!" << endl;
int main() {
Hello salute;
return 0;

This results in the following output:

Hello world!
Hello world!
Hello world!

All contributors

Looking to contribute?

Learn C++ on Codecademy