-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfunctor.cpp
56 lines (48 loc) · 974 Bytes
/
functor.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
class Vector {
private:
int x,y,z;
public:
Vector() : x(0), y(0), z(0) {}
Vector(int xx, int yy, int zz)
: x(xx), y(yy), z(zz) {}
virtual ~Vector() {}
/* Overload () op to use below inside Functor's class foo function */
bool operator()() {
return (x==y && x==z && y==z);
}
void clear() {
x=y=z=0;
}
};
class Functor {
private:
Vector v;
public:
Functor() {}
Functor(int a, int b, int c)
: v(a,b,c)
{}
~Functor() { v.clear(); }
/* Functors simple example using Vector's class overloaded operator () */
void foo() {
if( (v)() ) {
cout << "All values equal ... \n";
}
else {
cout << "Values not equal ... \n";
}
}
};
int main()
{
Functor f1;
Functor f2(2,2,2);
Functor f3(3,4,5);
f1.foo();
f2.foo();
f3.foo();
return 0;
}