So I've been taught (from "C++ Programming/ From Program Analysis to Design" book) to initialize my private variables in the constructor in the definition of a class but......I just played around and initialized a variable inside my class prototypes and it worked. Is it bad to do so or does it matter?
class enemy
{
public:
enemy();
void attackThePlayer(player& player);
void run(int, int);//take into account health and players level and such
void returnEnemyStats();//health, attack power and such;
private:
int health;
int defense;
int attackPower;
int bossesKilledCounter;
int enemiesKilledCounter;
bool isEnemyDead;
string getRandomEnemy;
vector<string> enemyList {"Scorpion", "Bug", "Hyena", "Wolf",
"Cat", "Shadow", "Dr. Fetus",
"Dark Link", "Majin Boo", "Dr. Mario",
"Dr. Mr. Evil"};
};
This is how I've been taught to initialize vars....inside your
class constructor:
1 2 3 4 5 6 7 8
enemy::enemy()
{
//11 different enemies as of now
enemyList = {"Scorpion", "Bug", "Hyena", "Wolf",
"Cat", "Shadow", "Dr. Fetus",
"Dark Link", "Majin Boo", "Dr. Mario",
"Dr. Mr. Evil"};
}
Both ways work just fine. Just wondering why have a constructor when
vars can be initialized inside a class prototype. Thanks.
EDIT - What's the difference between specifying a default param and initializing a member?