16.仿函数

1.function-like classes,所谓仿函数
一个对象能够像函数一样被调用,关键就是重载(重定义)operator()。
比如:
template
struct identity {
const T& operator()(const T& x) const {
return x;
}
};
使用时:
C++identity id; // 创建一个仿函数对象
int result = id(5); // 像调用函数一样:id(5),实际调用 operator()
cout << result << endl; // 输出 5
image

posted @ 2026-01-06 14:48  r5ett  阅读(3)  评论(0)    收藏  举报