Just another const WTF in I-17.2
Posted: December 18th, 2017, 2:13 pm
In Intermediate 17.2, I was playing around with the ApplyDamageTo() function to try to understand the const modifier.
So why am I able to modify target.name in this function? Does const only apply to the member data of this-> instance?
Code: Select all
void ApplyDamageTo(MemeFighter& target, int damage) const
{
std::cout << target.name << " takes " << damage << " damage." << std::endl;
// hp += damage; //can't do this because function is const
target.hp -= damage; //...BUT then why can target.hp be changed?
//Is the const modifier ONLY for this-> object?
if (!target.IsAlive())
{
std::cout << target.name << "Is dead, and starting to stink!" << std::endl;
}
}