Fonctionnement de struct operator()
Bonjour,
En lisant l'article sur Boost.Array, je suis tombé sur une syntaxe que je ne comprends pas:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| struct Init
{
int i;
Init() : i(0) { }
int operator() (int)
{
return i++;
}
};
struct TwoTimes
{
int operator() (int i)
{
return i*2;
}
};
int main()
{
boost::array<int,10> my_array;
transform(my_array.begin(),
my_array.end(),
my_array.begin(),
Init());
copy(my_array.begin(),my_array.end(),ostream_iterator<int>(cout," "));
cout << endl;
transform(my_array.begin(),
my_array.end(),
my_array.begin(),
TwoTimes());
copy(my_array.begin(),my_array.end(),ostream_iterator<int>(cout," "));
return 0;
} |
J'ai deux questions :
- Comment fonctionne le premier struct ?
- De l'appel eu 2ème transform, j'en déduis que TwoTimes est appelé pour chaque élément de my_array. Cela devrait être la même chose pour le premier appel à transform. Et pourtant, Init déclare i comme paramètre alors que TwoTimes ne le fait pas. Et pourtant les deux modifient la valeur de my_array qui leur est passée en "paramètre". Pourquoi ?
Merci par avance pour votre aide