In the below class, the member variable health_
is being initialised with the use of a colon, and before the constructor's main body:
class Monster
{
public:
virtual ~Monster() {}
protected:
Monster(int startingHealth)
: health_(startingHealth)
{}
private:
int health_; // Current health.
};
Why is health_
not initialised within the constructor's main body like this:
class Monster
{
public:
virtual ~Monster() {}
protected:
Monster(int startingHealth)
{
health_ = startingHealth;
}
private:
int health_; // Current health.
};
Is the first example different to the second?