c++ - How can I make an instant-dependant static variabe in a class method? -
i have function in class more or less works so:
class player { private: object* minions[16] public: void summon(object* obj); }; player::summon(object* obj) { static int = 0; if (i == 16) return; minions[i] = obj; i++; } the problem arise when trying use more 1 player, so:
player playerone; player playerthree; playerone.summon(new object("o1")); playerthree.summon(new object("o2")); o1 located in playerone.minions[0], expected, however, o2 located in playerthree.minions[1], summon() function using same i variable. there way make summon() function use static i variable single instance, use separate i variables each instance? know make for loop first spot in minions[] equal null, or make i member of player directly, want know if there better way before either of those.
change object* minions[16]; std::vector<object*> minions;. way can use minions.size() know how many there are, or minions.push_back(obj); add 1 without worrying array index stuff.
Comments
Post a Comment