operator()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Foo
{
public:
void operator() ()
{
cout <<"Foo operator"<<endl;
}
int operator() (int val)
{
return val*10;
}
};

Foo f;
f(); // Foo operator
cout<< f(5) <<endl; // 50

Foo是定义了调用操作符()的类,它的对象就相当于函数名,因此operator()取名叫函数对象


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
struct Data {
double dist;
double confidence;

bool operator <= ( double d ) const
{
return(dist < d);
}
bool operator <= ( Data& d ) const
{
return(dist <= d.dist);
}
};


Data a, b;
a.dist = 1;
a.confidence = 1;

b.dist = 1.3;
b.confidence = 1;
if(a <= 1.0)
cout << " 00000 " << endl;
if(a <= b)
cout << " 11111 " << endl;