我希望这个问题不是(太)主观的,因为我真的对“好”的方法很感兴趣,而且我还没有找到我在网上寻找的东西...所以我有一个C ++类,不太复杂,但有一堆属性。现在当我想通过类构造函数初始化它们时,我最终会得到类似的结果:
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();
}
我觉得这不是C ++做我想做的事情的正确方法。你如何处理如此长的一系列属性 - 或者甚至是“C ++方式”?
答案 0 :(得分:1)
考虑default member initialization:
class ADynamicGameMode
{
private:
NumSpawnPoints = 0;
ChallengeRating = 0;
....
或
class ADynamicGameMode
{
private:
NumSpawnPoints {0};
ChallengeRating {0};
....