Hello Everybody,
Most of my coding experience is in C. Although I have a background in C++, I know very little of STL and the advanced topics in C++.
I came across this code
struct g
{
g():n(0)
{
}
int operator()() { return n++; }
int n;
};
int main()
{
int a[10];
std::generate(a, a+10, g());
std::copy(a, a+10, std::ostream_iterator<int>(std::cout, " "));
getch();
return 0;
}
The output that I get is 0 1 2 3 4 5 6 7 8 9
Now this is my under standing of what is happening. The generate function calls the function g(). This function first assign n the value of 0
What I am unable to understand is how and where is the overloaded () operator called ? And how does it get called 10 times when the function g() is just called once.