class DoubleClass
1: //function_object.cpp
2: //Illustrates a "function object", which is just an object of
3: //a class that overloads the "function operator" operator().
4: //In other words, it's a class object that behaves like a function.
5: //The term "functor" is also used to mean "function object".
7: #include <iostream>
8: using namespace std;
10: class DoubleClass
11: {
12: public:
13: int operator()(int i)
14: {
15: return 2*i;
16: }
17: };
19: int main()
20: {
21: cout << "\nThis program illustrates a very simple function object, "
22: "or \"functor\". You need\nto study the source code to see "
23: "how the values shown below are produced.";
24: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
26: DoubleClass f;
28: cout << endl;
29: cout << f(6) << endl;
30: cout << f(-5) << endl;
32: //Actually the above syntax is shorthand for:
33: cout << f.operator()(6) << endl;
34: cout << f.operator()(-5) << endl;
35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
36: }