游戏// 无法更改重载构造函数的值
Game// Can't change the values of overloaded constructor
伙计们。
我倾斜了。
问题是每次转弯(我的攻击,敌人的攻击),我的和敌人的统计数据都会被重置。即敌人有 20 HP,我造成 5 DMG,它检测到变化,在下一次打印时敌人有 15 HP,但在下一个回合它的 HP 又回到 20。(英雄角色也会发生同样的事情。)
P.S 抱歉,英语不好,代码写得不好。这是完整的代码。
#pragma once
#include <string>
using namespace std;
class Hero
{
string Name;
int HP;
int DMG;
int LVL;
int EXP;
int MissChance;
bool HeroCheck;
public:
Hero(void);
~Hero(void);
void Hero_Print();
int BasicAttack();
int HeavyAttack();
void Heal();
void LVLUP();
int GetLVL();
void SetHP(int NewHP);
int GetHP();
int GetMissChance();
};
#pragma once
#include "Hero.h"
#include <iostream>
#include <string>
using namespace std;
Hero::Hero(void)
{
do{
cout<<"TYPE YOUR HERO'S NAME:"<<endl;
cin>>Name;
}while(Name.length()>10);
HP=20;
DMG=5;
LVL=1;
EXP=0;
MissChance=20;
HeroCheck=true;
}
Hero::~Hero(void)
{
}
void Hero::Hero_Print()
{
cout<<"____________"<<endl;
cout<<Name<<"'s STATISTICS"<<endl;
cout<<"HP: "<<HP<<endl;
cout<<"DMG: "<<DMG<<endl;
cout<<"LVL: "<<LVL<<endl;
cout<<"EXP: "<<EXP<<endl;
cout<<"____________"<<endl;
}
int Hero::BasicAttack()
{
return DMG;
}
int Hero::HeavyAttack()
{
return DMG+7;
}
void Hero::Heal()
{
HP=HP+GetLVL();
}
void Hero::LVLUP()
{
LVL++;
}
int Hero::GetLVL()
{
return LVL;
}
void Hero::SetHP(int NewHP)
{
HP=NewHP;
}
int Hero::GetHP()
{
return HP;
}
int Hero::GetMissChance()
{
return MissChance;
}
#pragma once
#include <string>
using namespace std;
class Enemy
{
string Name;
int HP;
int DMG;
int MissChance;
public:
Enemy();
void SetEnemy(string conName, int conHP, int conDMG, int conMissChance);
~Enemy(void);
void Enemy_Print();
int BasicAttack();
int SpecialAttack();
void Heal();
int GetMissChance();
void SetHP(int NewHP);
int GetHP();
string GetName();
};
#include "Enemy.h"
#include <string>
#include <iostream>
using namespace std;
Enemy::Enemy()
{
}
void Enemy::SetEnemy(string conName, int conHP, int conDMG, int conMissChance)
{
Name=conName;
HP=conHP;
DMG=conDMG;
MissChance=conMissChance;
}
Enemy::~Enemy(void)
{
}
void Enemy::Enemy_Print()
{
cout<<"____________"<<endl;
cout<<Name<<"'s Statistics"<<endl;
cout<<"HP: "<<HP<<endl;
cout<<"DMG: "<<DMG<<endl;
cout<<"____________"<<endl;
}
int Enemy::BasicAttack()
{
return DMG;
}
int Enemy::SpecialAttack()
{
return DMG+3;
}
void Enemy::Heal()
{
HP=HP+3;
}
void Enemy::SetHP(int NewHP)
{
HP=NewHP;
}
int Enemy::GetHP()
{
return HP;
}
int Enemy::GetMissChance()
{
return MissChance;
}
string Enemy::GetName()
{
return Name;
}
//All Battle Functions~~
void EnemyBattle(Hero Character, Enemy RandomEnemy);
void BasicAttack(Hero Character, Enemy RandomEnemy);
void SpecialAttack(Hero Character, Enemy RandomEnemy);
void HeroHeal(Hero Character);
void EnemyHeal(Enemy RandomEnemy);
int RandGen();
bool HPHero_Check(Hero Character);
bool HPEnemy_Check(Enemy RandomEnemy);
void Hero_Death(Hero Character);
void Enemy_Death(Enemy RandomEnemy);
void HeroAttacks_Couts(Hero Character);
int MakingTurn();
int MissChance();
void HeroBasicAttack(Hero Character, Enemy RandomEnemy);
void HeroHeavyAttack(Hero Character, Enemy RandomEnemy);
void HeroHeal(Hero Character);
void EnemyBasicAttack(Hero Character, Enemy RandomEnemy);
void EnemySpecialAttack(Hero Character, Enemy RandomEnemy);
void EnemyHeal(Enemy RandomEnemy);
void HeroTurn(Hero Character, Enemy RandomEnemy);
void EnemyTurn(Hero Character,Enemy RandomEnemy);
void SingleBattle(Hero Character,Enemy RandomEnemy);
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
int MakingTurn()
{
int TurnNum;
do
{
cout<<"Make your turn: ";
cin>>TurnNum;
}while(TurnNum<0||TurnNum>3);
system("cls");
return TurnNum;
}
int MissChance()
{
int RandomNum;
srand (time(NULL));
RandomNum = rand() % 100;
return RandomNum;
}
int RandGen()
{
int x;
srand(time(NULL));
x = rand()% 3+1;
return x;
}
void HeroAttacks_Couts(Hero Character)
{
cout<<"1.Basic Attack: "<<Character.BasicAttack()<<" DMG"<<endl;
cout<<"2.Heavy Attack: "<<Character.HeavyAttack()<<" DMG"<<endl;
cout<<"3.Heal yourself for "<<Character.GetLVL()*3<<" HP"<<endl;
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
void Enemy_Death(Enemy RandomEnemy)
{
system("cls");
RandomEnemy.Enemy_Print();
cout<<"You defeated the "<<RandomEnemy.GetName()<<"!"<<endl;
}
void Hero_Death(Hero Character)
{
system("cls");
Character.Hero_Print();
cout<<"You died!"<<endl;
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
bool HPEnemy_Check(Enemy RandomEnemy)
{
if(RandomEnemy.GetHP()<1)
return false;
else return true;
}
bool HPHero_Check(Hero Character)
{
if(Character.GetHP()<1)
return false;
else return true;
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
//My attacks
void HeroBasicAttack(Hero Character, Enemy RandomEnemy)
{
if(Character.GetMissChance()<MissChance())
{
int NewHP=RandomEnemy.GetHP()-Character.BasicAttack();
RandomEnemy.SetHP(NewHP);
cout<<"Your attack was successful!"<<endl;
RandomEnemy.Enemy_Print();
}else cout<<"You missed your attack!"<<endl;
}
void HeroHeavyAttack(Hero Character, Enemy RandomEnemy)
{
if(Character.GetMissChance()<MissChance())
{
int NewHP=RandomEnemy.GetHP()-Character.HeavyAttack();
RandomEnemy.SetHP(NewHP);
cout<<"Your attack was successful!"<<endl;
RandomEnemy.Enemy_Print();
}else cout<<"You missed your attack!"<<endl;
}
void HeroHeal(Hero Character)
{
Character.Heal();
cout<<"You successfully healed yourself!"<<endl;
Character.Hero_Print();
}
//Enemy attacks
void EnemyBasicAttack(Hero Character, Enemy RandomEnemy)
{
if(RandomEnemy.GetMissChance()<MissChance())
{
int NewHP=Character.GetHP()-RandomEnemy.BasicAttack();
Character.SetHP(NewHP);
cout<<"The "<<RandomEnemy.GetName()<<" hit you with his Basic Attack!"<<endl;
Character.Hero_Print();
}else cout<<"The "<<RandomEnemy.GetName()<<" missed you!"<<endl;
}
void EnemySpecialAttack(Hero Character,Enemy RandomEnemy)
{
if(RandomEnemy.GetMissChance()<MissChance())
{
int NewHP=Character.GetHP()-RandomEnemy.SpecialAttack();
Character.SetHP(NewHP);
cout<<"The "<<RandomEnemy.GetName()<<" hit you with his Special Attack!"<<endl;
Character.Hero_Print();
}else cout<<"The "<<RandomEnemy.GetName()<<" missed you!"<<endl;
}
void EnemyHeal(Enemy RandomEnemy)
{
RandomEnemy.Heal();
cout<<"The "<<RandomEnemy.GetName()<<" successfully used Heal!"<<endl;
RandomEnemy.Enemy_Print();
}
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
void HeroTurn(Hero Character, Enemy RandomEnemy)
{
switch(MakingTurn())
{
case 1:
HeroBasicAttack(Character,RandomEnemy);
break;
case 2:
HeroHeavyAttack(Character,RandomEnemy);
break;
case 3:
HeroHeal(Character);
break;
}
}
void EnemyTurn(Hero Character, Enemy RandomEnemy)
{
switch(RandGen())
{
case 1:
EnemyBasicAttack(Character,RandomEnemy);
break;
case 2:
EnemySpecialAttack(Character,RandomEnemy);
break;
case 3:
EnemyHeal(RandomEnemy);
break;
}
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
//Both mine and enemy's turns
void SingleBattle(Hero Character,Enemy RandomEnemy)
{
cout<<"You have encountered "<<RandomEnemy.GetName()<<"!Brace yourself for battle!"<<endl;
RandomEnemy.Enemy_Print();
do
{
if(!HPHero_Check(Character))
{
Hero_Death(Character);
break;
}else
Character.Hero_Print();
HeroAttacks_Couts(Character);
HeroTurn(Character,RandomEnemy);
if(!HPEnemy_Check(RandomEnemy))
{
Enemy_Death(RandomEnemy);
break;
}else
EnemyTurn(Character,RandomEnemy);
RandomEnemy.Enemy_Print();
}while(HPHero_Check(Character)||HPEnemy_Check(RandomEnemy));
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
// Selecting random enemy to fight
void EnemyBattle(Hero Character, Enemy RandomEnemy)
{
switch(RandGen())
{
case 1:
{
RandomEnemy.SetEnemy("Orc",18,7,30);
SingleBattle(Character,RandomEnemy);
break;
}
case 2:
{
RandomEnemy.SetEnemy("Skeleton",15,9,35);
SingleBattle(Character,RandomEnemy);
break;
}
case 3:
{
RandomEnemy.SetEnemy("Ghost",17,4,20);
SingleBattle(Character,RandomEnemy);
break;
}
}
}
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include "Hero.h"
#include "Enemy.h"
#include "Functions.h"
using namespace std;
//Main
int main()
{
Hero Character;
Enemy RandomEnemy;
bool GameRun = true;
do
{
Character.Hero_Print();
EnemyBattle(Character,RandomEnemy);
}while( GameRun);
return 0;
}
*
很简单,您是通过副本传递 Hero
和 Enemy
。这意味着复制了您的对象,damage/heal 应用于该对象,这对您要修改的对象没有影响。
看到这个问题:unable to change member value with method
int main()
{
Hero Character;
Character.Hero_Print();
// This passes a COPY of the character. The heal is applied and then you print the copies stats
HeroHeal(Character);
// However, no changes have been made to Character.
Character.Hero_Print();
}
您需要通过引用传递您的 Hero
// Takes a reference to the Hero
void HeroHeal(Hero& Character)
{
Character.Heal();
cout<<"You successfully healed yourself!"<<endl;
Character.Hero_Print();
}
现在不再传递副本,而是传递对 Hero
的引用。请注意,您调用函数的方式 不会改变 。也就是你还是会这样写:
// Heal the actual hero (parameter passed by reference)
void HeroHeal(Hero& Character);
// Heal a clone of this hero (parameter passed by copy)
void HeroHealClone(Hero Clone);
int main()
{
Hero newHero;
HeroHeal(newHero);
HeroHealClone(newHero);
}
伙计们。 我倾斜了。 问题是每次转弯(我的攻击,敌人的攻击),我的和敌人的统计数据都会被重置。即敌人有 20 HP,我造成 5 DMG,它检测到变化,在下一次打印时敌人有 15 HP,但在下一个回合它的 HP 又回到 20。(英雄角色也会发生同样的事情。) P.S 抱歉,英语不好,代码写得不好。这是完整的代码。
#pragma once
#include <string>
using namespace std;
class Hero
{
string Name;
int HP;
int DMG;
int LVL;
int EXP;
int MissChance;
bool HeroCheck;
public:
Hero(void);
~Hero(void);
void Hero_Print();
int BasicAttack();
int HeavyAttack();
void Heal();
void LVLUP();
int GetLVL();
void SetHP(int NewHP);
int GetHP();
int GetMissChance();
};
#pragma once
#include "Hero.h"
#include <iostream>
#include <string>
using namespace std;
Hero::Hero(void)
{
do{
cout<<"TYPE YOUR HERO'S NAME:"<<endl;
cin>>Name;
}while(Name.length()>10);
HP=20;
DMG=5;
LVL=1;
EXP=0;
MissChance=20;
HeroCheck=true;
}
Hero::~Hero(void)
{
}
void Hero::Hero_Print()
{
cout<<"____________"<<endl;
cout<<Name<<"'s STATISTICS"<<endl;
cout<<"HP: "<<HP<<endl;
cout<<"DMG: "<<DMG<<endl;
cout<<"LVL: "<<LVL<<endl;
cout<<"EXP: "<<EXP<<endl;
cout<<"____________"<<endl;
}
int Hero::BasicAttack()
{
return DMG;
}
int Hero::HeavyAttack()
{
return DMG+7;
}
void Hero::Heal()
{
HP=HP+GetLVL();
}
void Hero::LVLUP()
{
LVL++;
}
int Hero::GetLVL()
{
return LVL;
}
void Hero::SetHP(int NewHP)
{
HP=NewHP;
}
int Hero::GetHP()
{
return HP;
}
int Hero::GetMissChance()
{
return MissChance;
}
#pragma once
#include <string>
using namespace std;
class Enemy
{
string Name;
int HP;
int DMG;
int MissChance;
public:
Enemy();
void SetEnemy(string conName, int conHP, int conDMG, int conMissChance);
~Enemy(void);
void Enemy_Print();
int BasicAttack();
int SpecialAttack();
void Heal();
int GetMissChance();
void SetHP(int NewHP);
int GetHP();
string GetName();
};
#include "Enemy.h"
#include <string>
#include <iostream>
using namespace std;
Enemy::Enemy()
{
}
void Enemy::SetEnemy(string conName, int conHP, int conDMG, int conMissChance)
{
Name=conName;
HP=conHP;
DMG=conDMG;
MissChance=conMissChance;
}
Enemy::~Enemy(void)
{
}
void Enemy::Enemy_Print()
{
cout<<"____________"<<endl;
cout<<Name<<"'s Statistics"<<endl;
cout<<"HP: "<<HP<<endl;
cout<<"DMG: "<<DMG<<endl;
cout<<"____________"<<endl;
}
int Enemy::BasicAttack()
{
return DMG;
}
int Enemy::SpecialAttack()
{
return DMG+3;
}
void Enemy::Heal()
{
HP=HP+3;
}
void Enemy::SetHP(int NewHP)
{
HP=NewHP;
}
int Enemy::GetHP()
{
return HP;
}
int Enemy::GetMissChance()
{
return MissChance;
}
string Enemy::GetName()
{
return Name;
}
//All Battle Functions~~
void EnemyBattle(Hero Character, Enemy RandomEnemy);
void BasicAttack(Hero Character, Enemy RandomEnemy);
void SpecialAttack(Hero Character, Enemy RandomEnemy);
void HeroHeal(Hero Character);
void EnemyHeal(Enemy RandomEnemy);
int RandGen();
bool HPHero_Check(Hero Character);
bool HPEnemy_Check(Enemy RandomEnemy);
void Hero_Death(Hero Character);
void Enemy_Death(Enemy RandomEnemy);
void HeroAttacks_Couts(Hero Character);
int MakingTurn();
int MissChance();
void HeroBasicAttack(Hero Character, Enemy RandomEnemy);
void HeroHeavyAttack(Hero Character, Enemy RandomEnemy);
void HeroHeal(Hero Character);
void EnemyBasicAttack(Hero Character, Enemy RandomEnemy);
void EnemySpecialAttack(Hero Character, Enemy RandomEnemy);
void EnemyHeal(Enemy RandomEnemy);
void HeroTurn(Hero Character, Enemy RandomEnemy);
void EnemyTurn(Hero Character,Enemy RandomEnemy);
void SingleBattle(Hero Character,Enemy RandomEnemy);
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
int MakingTurn()
{
int TurnNum;
do
{
cout<<"Make your turn: ";
cin>>TurnNum;
}while(TurnNum<0||TurnNum>3);
system("cls");
return TurnNum;
}
int MissChance()
{
int RandomNum;
srand (time(NULL));
RandomNum = rand() % 100;
return RandomNum;
}
int RandGen()
{
int x;
srand(time(NULL));
x = rand()% 3+1;
return x;
}
void HeroAttacks_Couts(Hero Character)
{
cout<<"1.Basic Attack: "<<Character.BasicAttack()<<" DMG"<<endl;
cout<<"2.Heavy Attack: "<<Character.HeavyAttack()<<" DMG"<<endl;
cout<<"3.Heal yourself for "<<Character.GetLVL()*3<<" HP"<<endl;
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
void Enemy_Death(Enemy RandomEnemy)
{
system("cls");
RandomEnemy.Enemy_Print();
cout<<"You defeated the "<<RandomEnemy.GetName()<<"!"<<endl;
}
void Hero_Death(Hero Character)
{
system("cls");
Character.Hero_Print();
cout<<"You died!"<<endl;
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
bool HPEnemy_Check(Enemy RandomEnemy)
{
if(RandomEnemy.GetHP()<1)
return false;
else return true;
}
bool HPHero_Check(Hero Character)
{
if(Character.GetHP()<1)
return false;
else return true;
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
//My attacks
void HeroBasicAttack(Hero Character, Enemy RandomEnemy)
{
if(Character.GetMissChance()<MissChance())
{
int NewHP=RandomEnemy.GetHP()-Character.BasicAttack();
RandomEnemy.SetHP(NewHP);
cout<<"Your attack was successful!"<<endl;
RandomEnemy.Enemy_Print();
}else cout<<"You missed your attack!"<<endl;
}
void HeroHeavyAttack(Hero Character, Enemy RandomEnemy)
{
if(Character.GetMissChance()<MissChance())
{
int NewHP=RandomEnemy.GetHP()-Character.HeavyAttack();
RandomEnemy.SetHP(NewHP);
cout<<"Your attack was successful!"<<endl;
RandomEnemy.Enemy_Print();
}else cout<<"You missed your attack!"<<endl;
}
void HeroHeal(Hero Character)
{
Character.Heal();
cout<<"You successfully healed yourself!"<<endl;
Character.Hero_Print();
}
//Enemy attacks
void EnemyBasicAttack(Hero Character, Enemy RandomEnemy)
{
if(RandomEnemy.GetMissChance()<MissChance())
{
int NewHP=Character.GetHP()-RandomEnemy.BasicAttack();
Character.SetHP(NewHP);
cout<<"The "<<RandomEnemy.GetName()<<" hit you with his Basic Attack!"<<endl;
Character.Hero_Print();
}else cout<<"The "<<RandomEnemy.GetName()<<" missed you!"<<endl;
}
void EnemySpecialAttack(Hero Character,Enemy RandomEnemy)
{
if(RandomEnemy.GetMissChance()<MissChance())
{
int NewHP=Character.GetHP()-RandomEnemy.SpecialAttack();
Character.SetHP(NewHP);
cout<<"The "<<RandomEnemy.GetName()<<" hit you with his Special Attack!"<<endl;
Character.Hero_Print();
}else cout<<"The "<<RandomEnemy.GetName()<<" missed you!"<<endl;
}
void EnemyHeal(Enemy RandomEnemy)
{
RandomEnemy.Heal();
cout<<"The "<<RandomEnemy.GetName()<<" successfully used Heal!"<<endl;
RandomEnemy.Enemy_Print();
}
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
void HeroTurn(Hero Character, Enemy RandomEnemy)
{
switch(MakingTurn())
{
case 1:
HeroBasicAttack(Character,RandomEnemy);
break;
case 2:
HeroHeavyAttack(Character,RandomEnemy);
break;
case 3:
HeroHeal(Character);
break;
}
}
void EnemyTurn(Hero Character, Enemy RandomEnemy)
{
switch(RandGen())
{
case 1:
EnemyBasicAttack(Character,RandomEnemy);
break;
case 2:
EnemySpecialAttack(Character,RandomEnemy);
break;
case 3:
EnemyHeal(RandomEnemy);
break;
}
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
//Both mine and enemy's turns
void SingleBattle(Hero Character,Enemy RandomEnemy)
{
cout<<"You have encountered "<<RandomEnemy.GetName()<<"!Brace yourself for battle!"<<endl;
RandomEnemy.Enemy_Print();
do
{
if(!HPHero_Check(Character))
{
Hero_Death(Character);
break;
}else
Character.Hero_Print();
HeroAttacks_Couts(Character);
HeroTurn(Character,RandomEnemy);
if(!HPEnemy_Check(RandomEnemy))
{
Enemy_Death(RandomEnemy);
break;
}else
EnemyTurn(Character,RandomEnemy);
RandomEnemy.Enemy_Print();
}while(HPHero_Check(Character)||HPEnemy_Check(RandomEnemy));
}
#pragma once
#include "Enemy.h"
#include "Hero.h"
#include "Functions.h"
#include <iostream>
#include <string>
using namespace std;
// Selecting random enemy to fight
void EnemyBattle(Hero Character, Enemy RandomEnemy)
{
switch(RandGen())
{
case 1:
{
RandomEnemy.SetEnemy("Orc",18,7,30);
SingleBattle(Character,RandomEnemy);
break;
}
case 2:
{
RandomEnemy.SetEnemy("Skeleton",15,9,35);
SingleBattle(Character,RandomEnemy);
break;
}
case 3:
{
RandomEnemy.SetEnemy("Ghost",17,4,20);
SingleBattle(Character,RandomEnemy);
break;
}
}
}
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include "Hero.h"
#include "Enemy.h"
#include "Functions.h"
using namespace std;
//Main
int main()
{
Hero Character;
Enemy RandomEnemy;
bool GameRun = true;
do
{
Character.Hero_Print();
EnemyBattle(Character,RandomEnemy);
}while( GameRun);
return 0;
}
*
很简单,您是通过副本传递 Hero
和 Enemy
。这意味着复制了您的对象,damage/heal 应用于该对象,这对您要修改的对象没有影响。
看到这个问题:unable to change member value with method
int main()
{
Hero Character;
Character.Hero_Print();
// This passes a COPY of the character. The heal is applied and then you print the copies stats
HeroHeal(Character);
// However, no changes have been made to Character.
Character.Hero_Print();
}
您需要通过引用传递您的 Hero
// Takes a reference to the Hero
void HeroHeal(Hero& Character)
{
Character.Heal();
cout<<"You successfully healed yourself!"<<endl;
Character.Hero_Print();
}
现在不再传递副本,而是传递对 Hero
的引用。请注意,您调用函数的方式 不会改变 。也就是你还是会这样写:
// Heal the actual hero (parameter passed by reference)
void HeroHeal(Hero& Character);
// Heal a clone of this hero (parameter passed by copy)
void HeroHealClone(Hero Clone);
int main()
{
Hero newHero;
HeroHeal(newHero);
HeroHealClone(newHero);
}