It's far too annoying to attempt to define ChampionStats
instances inside the ChampionStats
class. They would have to be made static
or every instance would contain all of the pre-built champions. That includes the pre-built champions themselves, and that's a particularly nasty bit of recursion that the compiler's going to shut right down.
But if you make them static
, you're back to defining the instances outside of the class.
Instead I pitch a namespace as a simple direct solution.
namespace champions
{
ChampionStats Fred ({1, "Fred", 42, 96);
ChampionStats Abby ({2, "Abby", 88, 11);
ChampionStats Stinky({3, "Stinky", 18, 77);
}
But a container might scale a bit better.
int nextId = 0; // keep track of the next available ID
map<string, ChampionStats> champs =
{
{"Fred", {++nextId, "Fred", 42, 96}},
{"Abby", {++nextId, "Abby", 88, 11}},
{"Stinky", {++nextId, "Stinky", 18, 77}},
};
You can keep adding rows to the list until the cows come home and nothing else needs to change.
Usage:
int main()
{
champs.at("Fred").printChamp();
champs.at("Abby").printChamp();
}
But it's clunky. The name is repeated twice, making it really easy to typo or cut-n-paste-n-forget. There's probably a better way to pull this off with some groovy template voodoo, but I'm no wizard. Here I'd use a macro.
int nextId = 0; // keep track of the next available ID
#define BUILD_CHAMP(name, hp, ad) {name,{++nextId, name, hp, ad}}
map<string, ChampionStats> champs =
{
BUILD_CHAMP("Fred", 42, 96),
BUILD_CHAMP("Abby", 88, 11),
BUILD_CHAMP("Stinky", 18, 77),
};