Re: What is this function object?
On 11 5 , 1 13 , Porkling <porkl...@yeah.net> wrote:
Here,PrintInt(),is a function object yet.
It runs constructor first (doing nothing here) , and runs operator()
later.
On Mon, 05 Nov 2007 04:58:13 -0000, "webinfin...@gmail.com"
<webinfin...@gmail.com> wrote:
Could anyone explain this snippet for me?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};
int main() {
vector<int> coll;
for (int i = 1; i <=9; ++i)
coll.push_back(i);
for_each (coll.begin(), coll.end(), PrintInt()); // I don't
understand here
}
for_each needs a function object, so if we are doing: PrintInt p, then
we call for_each(coll.begin(), coll.end(), p()); I will understand,
but what is this "PrintInt()" here? Is it a default constructor? Or
just a call to overloaded operator ()?
Thank you for your response.- -
- -
accurately, opeartor() is called inside for_each, not immediately
after default constructor