How to make this C++ code more DRY?
- by Macha
I have these two methods on a class that differ only in one method call. Obviously, this is very un-DRY, especially as both use the same formula.
int PlayerCharacter::getAttack() {
attack = 1 + this.level;
for(int i = 0; i <= current_equipment; i++) {
attack += this.equipment[i].getAttack();
}
attack *= sqrt(this.level);
return attack;
}
int PlayerCharacter::getDefense() {
defense = 1 + this.level;
for(int i = 0; i <= current_equipment; i++) {
defense += this.equipment[i].getDefense();
}
defense *= sqrt(this.level);
return defense;
}
How can I tidy this up in C++?