首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

函数对象与构造函数的有关问题!

2013-08-24 
函数对象与构造函数的问题!!!#include iostream#include algorithmusing namespace stdclass LessTha

函数对象与构造函数的问题!!!

#include <iostream>
#include <algorithm>
using namespace std;
class LessThan
{
public:
LessThan(int sz=6):_size(sz){}
bool operator()(int a)
{
return a<_size;
}
private:
int _size;
};

void main()
{
int pa[]={6,9,10,22,2,0};
int *iter=pa;
if (LessThan(6)(2))
{
cout << "true";
}
while((iter=find_if(iter,pa+6,LessThan(11)))!=pa+6)
{
cout << " "<< *iter << " ";
iter++;
}
system("pause");
}

如上的程序,在while语句中的LessThan(11)语句为啥能生成函数指针,它并没有使用函数调用操作符()啊,望高手解惑! c++ 构造函数 函数对象
[解决办法]
引用:
Quote: 引用:

LessThan(6)(2) 中第二对括号就是调用 LessThan::operator()

是啊。我想问的是while中LessThan(11)的问题,这个并没有第二对括号。

sorry,之前看错了。
find_if 内部会把 LessThan(11) 生成的对象当函数调用,像这样
LessThan lt(11);
lt(some_value_of_pa); // 这里调用 LessThan::operator()

热点排行