struct absInt
{
int operator() (int val)
{
return val < 0 ? -val : val;
}
};
class Funclass
{
public:
Funclass(){};
bool operator()(CStr& mcstr)
{
std::cout<<mcstr.getStr()<<endl;
return true;
}
};
int i = -42;
absInt absObj; // object that defines function call operator
unsigned int ui = absObj(i); // calls absInt::operator(int)
class GT_cls {
public:
GT_cls(size_t val = 0): bound(val) { }
bool operator()(const string &s)
{ return s.size() >= bound; }
private:
std::string::size_type bound;
};
本文介绍并展示了C++中使用函数对象的概念与实践,包括绝对值计算、字符串输出及字符串长度比较等功能的实现。
429

被折叠的 条评论
为什么被折叠?



