0

I hope this question is not (too) subjective, as I am really interested in "a good" approach, and I haven't found what I am looking for online... So I have a C++ class, not too complex, but with a bunch of attributes. Now when I want to initialize them all via the class constructor, I end up with something like this:

ADynamicGameMode::ADynamicGameMode() : NumSpawnPoints(0),
                                       ChallengeRating(0),
                                       MinNumberOfBadBugsOnMap(0),
                                       MaxNumberOfBadBugsTotal(0),
                                       NumberOfBadBugsSpawned(0),
                                       NumberOfBadBugsKilled(0),
                                       BadBugSpawnInterval(0),
                                       CommonSpidersKilled(0),
                                       PoisonousSpiderKilled(0),
                                       SniperSpiderKilled(0),
                                       BigAssSpiderKilled(0),
                                       MinChallengeRating(CR_MIN),
                                       MaxChallengeRating(CR_MAX),
                                       SpawnIntervalBaseValue(10),
                                       MinBadBugsBaseValue(2),
                                       MaxBadBugsBaseValue(10),
                                       SpawnIntervalLoopModifier(0.5),
                                       SpawnIntervalCrModifier(1.0),
                                       MinBadBugsCrModifier(1.0),                                                   MaxBadBugsCrModifierCommon(0.8),                                           MaxBadBugsCrModifierPoisonous(1.0),
                                       MaxBadBugsCrModifierBigAss(1.3),
                                       MaxBadBugsCrModifierSniper(1.5),
                                       CommonCrBoundary(0),
                                       PoisonousCrBoundary(20),
                                       BigAssCrBoundary(40),
                                       SniperCrBoundary(55),
                                       FireflyPawn(nullptr),
                                       BP_CommonSpider(nullptr),
                                       BP_PoisonousSpider(nullptr),
                                       BP_BigAssSpider(nullptr),
                                       BP_SniperSpider(nullptr)
{
    LastBadBugSpawnTime = std::numeric_limits<float>::lowest();
}

I feel that this is not the proper way in C++ of doing what I want. How do you deal with such a long list of attribues -- or is there even "the" C++ way?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • "I feel that this is not the proper way in C++ of doing what I want. " - and you feel that way because.. ? This *is* the proper way to do this if you hope to (a) have proper initial values, and (b) want to avoid default-initialization followed by an equally lengthy direct-assignment list (which itself isn't an option for members that are either `const` or references). Something worth consideration is whether the encompassing class is really, *really* designed well with 30+ member variables. Maybe consider doing something about that. – WhozCraig Aug 28 '16 at 21:54
  • @WhozCraig: I was waiting for that comment ... Well, I understand that you ask whether such a large list of member variables makes sense. For many cases I would agree with you, but in this case I don't. Because this class does nothing else than tracking various stats about the running program (game), more or less a bunch of global counters and alike. In our case it doesn't really make sense to split that into several classes and/or structures. – Matthias Aug 28 '16 at 22:02
  • So long as you understand that you're your members are default-constructing, *then* direct-assigning if you move this down into the constructor body, and that some member var types *cannot* do that, which you choose is up to you. There is no real performance hit for intrinsic types. [An excellent article is here](https://isocpp.org/wiki/faq/ctors#init-lists). Alok's answer to [this question](http://stackoverflow.com/a/8523361/1322972), in particular the section on when you *must* use member-initializaiton, is equally good. – WhozCraig Aug 28 '16 at 22:06

1 Answers1

1

Consider default member initialization:

class ADynamicGameMode
{
private:
    NumSpawnPoints = 0;
    ChallengeRating = 0;
    ....

or

class ADynamicGameMode
{
private:
    NumSpawnPoints {0};
    ChallengeRating {0};
    ....
AlexD
  • 32,156
  • 3
  • 71
  • 65