Alternative 1, réutiliser une variable temporaire:mode préféré de remplissage d'un vecteur C++ de structs
Sticker sticker;
sticker.x = x + foreground.x;
sticker.y = foreground.y;
sticker.width = foreground.width;
sticker.height = foreground.height;
board.push_back(sticker);
sticker.x = x + outline.x;
sticker.y = outline.y;
sticker.width = outline.width;
sticker.height = outline.height;
board.push_back(sticker);
Alternative 2, la portée de la variable temporaire:
{
Sticker sticker;
sticker.x = x + foreground.x;
sticker.y = foreground.y;
sticker.width = foreground.width;
sticker.height = foreground.height;
board.push_back(sticker);
}
{
Sticker sticker;
sticker.x = x + outline.x;
sticker.y = outline.y;
sticker.width = outline.width;
sticker.height = outline.height;
board.push_back(sticker);
}
Alternative 3, écrire directement à la mémoire vectorielle:
{
board.push_back(Sticker());
Sticker &sticker = board.back();
sticker.x = x + foreground.x;
sticker.y = foreground.y;
sticker.width = foreground.width;
sticker.height = foreground.height;
}
{
board.push_back(Sticker());
Sticker &sticker = board.back();
sticker.x = x + outline.x;
sticker.y = outline.y;
sticker.width = outline.width;
sticker.height = outline.height;
}
Quelle approche préférez-vous?
Edit: Pour des raisons de cette discussion, on suppose que les missions doivent être effectuées par un à l'extérieur d'un constructeur
Ma suggestion, mais je préfère 'board.push_back (autocollant (contour, x)); board.push_back (Sticker (premier plan, x)) 'plutôt que de passer chaque pièce une par une. –
Ya, ajouter une liste init à l'intérieur et voila ' – Maciek
Comment appelleriez-vous vos paramètres constructeur, car ils devraient idéalement avoir les mêmes noms que les membres de la classe? – henle