example1_basic_functor.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

class Multiplier {
private:
    int factor;

public:
    Multiplier(int f) : factor(f) {}

    int operator()(int x) const {
        return x * factor;
    }
};

int main() {
    Multiplier times3(3);
    std::cout << "5 * 3 = " << times3(5) << std::endl;
    std::cout << "7 * 3 = " << times3(7) << std::endl;

    return 0;
}
Back to functor