1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <iostream>
#include <memory>
class Character {
public:
Character(std::string name, int health) {
std::cout << "Character" << name << ' ' << health << std::endl;
}
virtual ~Character() {
std::cout << "~Character" << std::endl;
}
};
class Warrior : public Character {
public:
enum Weapon {
AXE, SWORD, SPEAR
};
Warrior(std::string name, int health, Weapon weapon) :
Character(name, health) {
std::cout << "Warrior" << ' ' << weapon << std::endl;
}
~Warrior() {
std::cout << "~Warrior" << std::endl;
}
};
class Archer : public Character {
public:
Archer(std::string name, int health, int arrowCount) :
Character(name, health) {
std::cout << "Archer" << ' ' << arrowCount << std::endl;
}
~Archer() {
std::cout << "~Archer" << std::endl;
}
};
template<typename TypeCharacter, typename ... CharacterParam>
std::unique_ptr<TypeCharacter> createCharacter(CharacterParam&&... param) {
return std::make_unique<TypeCharacter>(param...);
}
int main() {
std::cout << "Go!" << std::endl;
{
auto warrior = createCharacter<Warrior>("Ragnar", 100, Warrior::AXE);
auto archer = createCharacter<Archer>("Legolas", 80, 42);
}
std::cout << "Game over" << std::endl;
} |
Partager