一切运行正常,但我的问题是我的主要功能太长了。有没有办法在主要功能之外创建战斗场景?我试过这个,但如果我把战斗场景 - 让我们说“战斗()” - 在头文件或其他东西,然后我的对象创建一个错误。有没有办法让我在main函数之外的main工作中声明的对象。也许使用指针或东西?
int main()
{
Hero Me(100,20,30,40);//Created using overloaded constructor
Monster m(100,16,18,20);//creates a monster object and uses overloaded constructor to initialize
cout << "\nAttacking!\n";
//I want this part to be in a function called Battle() outside of main
while ((Me.getHp() > 0) && (m.getHp() > 0))//Generates error if object not declared in main
{
cout << "\nYour hp is: " << Me.getHp() << endl;
cout << "The enemy's hp is: "<< m.getHp() << endl;
cout << "\nThe monster has attacked you!\n";
cout << "You received " << m.getAttack() << " damage;" << endl;
Me.damageTaken(m.getAttack());//Me.setHp(Me.getHp() - m.getAttack());//Me.setHp(m.getStrength());
if(Me.getHp() > 0)//Check if still alive
{
cout << "\nYour hp is now: " << Me.getHp() << endl;
//cout << "Enemy hp is: "<< m.getHp() << endl;
cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
m.damageTaken(Me.getAttack());//m.setHp(m.getHp() - Me.getAttack());//m.setHp(Me.getAttack());
if(m.getHp() > 0)//Check if still alive
{
cout << "Enemy hp is now: " << m.getHp() << endl;
cout << "\nAttacking again!\n";
}
}
}
if ((Me.getHp() > 0) && (m.getHp() <= 0))
cout <<"\nCongratulations! You killed the enemy!" << endl;
else if ((Me.getHp() <= 0) && (m.getHp() > 0))
cout << "You have died!" << endl;
cin.sync();
cin.get();
return 0;
}
如果代码:
,那么剩下的就是其余的//Hero.h
class Hero:
public Character
{
public:
Hero();
Hero(int, int, int, int);
~Hero();
};
//Hero.cpp
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: Character(newHp, newLevel, newAttack, newDef)
{
cout << "Hero created using Overloaded function!\n";
hp = newHp;
cout << "Hp is: "<< hp << endl;
level = newLevel;
cout << "level is: " << level << endl;
attack = newAttack;
cout << "Attack is: " << attack << endl;
defense = newDef;
cout << "Defense is: " << defense << endl;
// logging goes here
// note that you don't need HeroLevel etc. at all any more, just use level
}
Hero::~Hero()
{
cout << "Hero destroyed!\n";
}
//Monster.h
class Monster:
public Character //Hero
{
public:
Monster();
Monster(int, int, int, int); //explicit
//:Character(){};
//Monster(int);
~Monster();
};
//Monster.cpp
Monster::Monster(int newHp, int newLevel, int newAttack, int newDef)
//: hp(newHp), level(newLevel), attack(newAttack), defense(newDef)//initialize list
{
cout << "Monster created using Overloaded function!\n";
hp = newHp;
cout << "Hp is: "<< hp << endl;
level = newLevel;
cout << "level is: " << level << endl;
attack = newAttack;
cout << "Attack is: " << attack << endl;
defense = newDef;
cout << "Defense is: " << defense << endl;
}
Monster::~Monster()
{
cout << "\nMonster Destroyed";
}
//Character.h
class Character
{
protected://not private, so derived class can use it
int level;
int hp;
int attack;
int defense;
Character(); // zero everything by default
Character(int); // randomly generate everything
Character(int, int, int, int); // populate explicitly
~Character();
public:
int getAttack() const { return attack; }
int getDefense() const { return defense; }
int getHp() const { return hp; }
int getlevel() const { return level; }
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setlevel(int);
void damageTaken(int);
//Character.cpp
Character::Character() : level(0), hp(0), attack(0), defense(0) {}
Character::Character(int hit, int lvl, int att, int def)
: level(lvl), hp(hit), attack(att), defense(def){}
Character::~Character()
{
cout << "Character has been destroyed!\n";
}
答案 0 :(得分:2)
你可以用三种方式做到:
返回副本。这可能是首选方式,对于简单对象来说最简单。
这种功能的例子:
Hero create_hero()
{
Hero hero{...};
// Other stuff
return hero;
}
返回参考文献。这里最大的警告是你不能返回对局部变量的引用,你必须返回对其他地方存储的数据的引用(例如在向量中)。
返回指针。这与数字2具有相同的警告,返回指向局部变量的指针,例如, return &some_local_var;
是一个坏主意。
这种功能的例子:
std::unique_ptr<Hero> create_hero()
{
auto hero = std::unique_ptr<Hero>(new Hero{...});
// other stuff
// remember that `hero` is a pointer, so the `->` operator has to be used
// when dereferencing the variable
// e.g. `hero->some_function()`
return hero;
}
对于第1点,如果你有任何类型的复杂对象,你应该知道the rule of three。
对于第3点,我建议使用智能指针,例如std::unique_ptr
或std::shared_ptr
,如我的示例所示。