#include <typeinfo>
#include "warrior.h"
#include "magician.h"
#ifndef MAN_H
#define MAN_H
class man
{
public:
man();
virtual ~man();
int get_hp();
int get_mp();
int get_level();
template <class T> void upgrade(T *t,int n);
protected:
int hp;
int mp;
int level;
};
template <class T>
void man::upgrade(T *t,int n){
t->level = t->level +1;
//原本的解法
/*
if(n==1){
t->hp = t->hp*2;
t->mp = t->mp*3;
}
else if(n==2){
t->hp = t->hp*3;
t->mp = t->mp*2;
}*/
//想改成類似下面這種
if(typeid(t) == typeid(magician)){
t->hp = t->hp*2;
t->mp = t->mp*3;
}
else if(typeid(t) == typeid(warrior)){
t->hp = t->hp*3;
t->mp = t->mp*2;
}
}
#endif // MAN_H
I2luY2x1ZGUgPHR5cGVpbmZvPgojaW5jbHVkZSAid2Fycmlvci5oIgojaW5jbHVkZSAibWFnaWNpYW4uaCIKI2lmbmRlZiBNQU5fSAojZGVmaW5lIE1BTl9ICgpjbGFzcyBtYW4KewogICAgcHVibGljOgogICAgICAgIG1hbigpOwogICAgICAgIHZpcnR1YWwgfm1hbigpOwogICAgICAgIGludCBnZXRfaHAoKTsKICAgICAgICBpbnQgZ2V0X21wKCk7CiAgICAgICAgaW50IGdldF9sZXZlbCgpOwogICAgICAgIHRlbXBsYXRlIDxjbGFzcyBUPiB2b2lkIHVwZ3JhZGUoVCAqdCxpbnQgbik7CgogICAgcHJvdGVjdGVkOgogICAgICAgIGludCBocDsKICAgICAgICBpbnQgbXA7CiAgICAgICAgaW50IGxldmVsOwp9OwoKdGVtcGxhdGUgPGNsYXNzIFQ+CnZvaWQgbWFuOjp1cGdyYWRlKFQgKnQsaW50IG4pewogICAgdC0+bGV2ZWwgPSB0LT5sZXZlbCArMTsKCgkvL+WOn+acrOeahOino+azlQogICAgLyoKCWlmKG49PTEpewogICAgICAgIHQtPmhwID0gdC0+aHAqMjsKICAgICAgICB0LT5tcCA9IHQtPm1wKjM7CiAgICB9CiAgICBlbHNlIGlmKG49PTIpewogICAgICAgIHQtPmhwID0gdC0+aHAqMzsKICAgICAgICB0LT5tcCA9IHQtPm1wKjI7CiAgICB9Ki8KCgkvL+aDs+aUueaIkOmhnuS8vOS4i+mdoumAmeeorgogICAgaWYodHlwZWlkKHQpID09IHR5cGVpZChtYWdpY2lhbikpewogICAgICAgIHQtPmhwID0gdC0+aHAqMjsKICAgICAgICB0LT5tcCA9IHQtPm1wKjM7CiAgICB9CiAgICBlbHNlIGlmKHR5cGVpZCh0KSA9PSB0eXBlaWQod2FycmlvcikpewogICAgICAgIHQtPmhwID0gdC0+aHAqMzsKICAgICAgICB0LT5tcCA9IHQtPm1wKjI7CiAgICB9Cn0KI2VuZGlmIC8vIE1BTl9I