Sorry for being a little dense—I'm just not quite wrapping my head around this. (huehuehuehuehuehuehue)
class Item {
Item(std::string name) : durability_(100) { name_ = name; }
void break() { std::cout << "Your item broke!"; }
int durability_;
std::string name_;
};
// To-be wrapped
class Weapon : public Item {
Weapon(std::string name) : Item(name) {}
void chip() { durability_ -= 5; }
void rust() { durability_ -= 7; }
}
// Wrapper
class StrongWeapon : public Item {
StrongWeapon(std::string name) : Item(name) {}
Weapon* baseWeapon = new Weapon;
void chip() { durability_ -= 2; }
};
// Wrapper
class GlowingWeapon : public Item {
GlowingWeapon(std::string name) : Item(name) {}
Weapon* baseWeapon = new Weapon;
void glow() { std::cout << "You're blinded by your own weapon!"; }
};
Do I have the setup right?
The idea is that I can now have a strong weapon, a glowing weapon, or a strong-glowing weapon, right? I've seen code like this in all the tutorials, but it seems to either go on for 350 more lines, or just stop there.
I guess I'm not sure how to use these wrappers. How does a GlowingWeapon access the rust() method? Is it going to be something like: GlowingWeapon->baseWeapon->rust() ? Or just GlowingWeapon->rust() ? If it's the latter case, how (and why) does this work? How do I create a Strong, Glowing Weapon?
Also, is it true that anything that takes a Weapon (or a pointer to a Weapon) will also be able to take a GlowingWeapon, or StrongWeapon (or both?)
I understand that "one class wraps another to change functionality," but beyond that I'm totally confused, and having a really hard time finding clear, succinct tutorials.
(Thanks for your patience, guys.)