ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

16.仿函数

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

返回列表