constructors

Why in this piece of code he is using a private attribute of a class as a constructor?

http://www.cs.fsu.edu/~lacher/courses/COP5517/lectures/loki5/slide10.html

"ResultType operator () () { return fun_(); }"
The private data member fun_ (which you call "attribute") is supposed to be a function, as is suggested by its type name "Fun".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

template <typename Function>
class FunctionWrapper
{
public:

    explicit FunctionWrapper(Function f): f(f)
    {
    }

    void call() const
    {
        f();
    }

private:

    Function f;
};

void print_hello()
{
    std::cout << "Hello";
}

void print_world()
{
    std::cout << "World";
}

int main()
{
    FunctionWrapper<void(*)()> fw1(&print_hello);
    FunctionWrapper<void(*)()> fw2(&print_world);

    fw1.call();
    fw2.call();
}
HelloWorld

Thanks for all...now i understand all!!! =))
Topic archived. No new replies allowed.