如何从 C++ 中的另一个实例访问一个实例的信息?
How to access info from an instance from another instance in c++?
我刚开始学习 OOP 概念,为了帮助自己学习,我创建了一个角色 class。从这个 class 我创建了一个名为 main 的实例和一个名为 monster 的实例。这是 class:
的代码
#include <iostream>
#include <string>
using namespace std;
class Character {
public:
string name;
float health;
int attackLevel;
int defenseLevel;
void setAttr(string sName,float sHealth, int sAttackLevel, int sDefenseLevel) {
name = sName;
health = sHealth;
attackLevel = sAttackLevel;
defenseLevel = sDefenseLevel;
}
void attack(int whatInstanceToAttack) {
whatInstanceToAttack.hitpoints -= 20; //obviously not valid but how do i do this?
return whatInstanceToAttack;
}
int defend(string defend) {
int damageRelieved = defenseLevel * 2;
return damageRelieved;
}
};
int main() {
Character main;
Character monster;
main.setAttr("Rafael",200,100,30);
monster.setAttr("Monster1",30,40,30);
cout << "Default Values for Raf are;" << endl;
cout << main.name << endl;
cout << main.health<< endl;
cout << main.attackLevel << endl;
cout << main.defenseLevel << endl;
cout << "Default values for monster are" << endl;
cout <<monster.name << endl;
cout <<monster.health << endl;
cout << monster.attackLevel<< endl;
cout << monster.defenseLevel << endl;
return 0;
}
基本上我想做的是以某种方式通过主实例访问怪物实例。我想通过 运行ning 攻击方法来做到这一点。所以如果我 运行
main.attack(monster);
那我想让怪物失去20点生命值。
我该怎么做?
重载方法攻击,您可以根据需要按值或引用传递。
虚空攻击(角色chr)
要么
void attack(字符 &chr)
你只需要在攻击方法中传递Character的引用即可。
我认为您必须了解按值传递和按引用传递概念。如果没有,你可以阅读它 here
void attack(Character &whatInstanceToAttack) {
whatInstanceToAttack.hitpoints -= 20; //obviously not valid but how do i do this?
}
是的,您可以从同一个 class 的另一个实例访问一个实例的变量。您需要使用对该对象的引用以确保更改反映在另一个实例中。所以这就是你的攻击函数应该是什么样子的。
void attack(Character &c)
{
c.hitpoints - = 20;
}
现在,当您从 main()
函数调用 main.attack(monster) 时,monster 的 hitpoints
将减少 20。
作为旁注,将 class 的数据成员设为私有被认为是一种很好的做法,以避免数据的非法 access/modification。始终将成员函数用作 class 个实例的接口。
我刚开始学习 OOP 概念,为了帮助自己学习,我创建了一个角色 class。从这个 class 我创建了一个名为 main 的实例和一个名为 monster 的实例。这是 class:
的代码#include <iostream>
#include <string>
using namespace std;
class Character {
public:
string name;
float health;
int attackLevel;
int defenseLevel;
void setAttr(string sName,float sHealth, int sAttackLevel, int sDefenseLevel) {
name = sName;
health = sHealth;
attackLevel = sAttackLevel;
defenseLevel = sDefenseLevel;
}
void attack(int whatInstanceToAttack) {
whatInstanceToAttack.hitpoints -= 20; //obviously not valid but how do i do this?
return whatInstanceToAttack;
}
int defend(string defend) {
int damageRelieved = defenseLevel * 2;
return damageRelieved;
}
};
int main() {
Character main;
Character monster;
main.setAttr("Rafael",200,100,30);
monster.setAttr("Monster1",30,40,30);
cout << "Default Values for Raf are;" << endl;
cout << main.name << endl;
cout << main.health<< endl;
cout << main.attackLevel << endl;
cout << main.defenseLevel << endl;
cout << "Default values for monster are" << endl;
cout <<monster.name << endl;
cout <<monster.health << endl;
cout << monster.attackLevel<< endl;
cout << monster.defenseLevel << endl;
return 0;
}
基本上我想做的是以某种方式通过主实例访问怪物实例。我想通过 运行ning 攻击方法来做到这一点。所以如果我 运行
main.attack(monster);
那我想让怪物失去20点生命值。
我该怎么做?
重载方法攻击,您可以根据需要按值或引用传递。
虚空攻击(角色chr) 要么 void attack(字符 &chr)
你只需要在攻击方法中传递Character的引用即可。 我认为您必须了解按值传递和按引用传递概念。如果没有,你可以阅读它 here
void attack(Character &whatInstanceToAttack) {
whatInstanceToAttack.hitpoints -= 20; //obviously not valid but how do i do this?
}
是的,您可以从同一个 class 的另一个实例访问一个实例的变量。您需要使用对该对象的引用以确保更改反映在另一个实例中。所以这就是你的攻击函数应该是什么样子的。
void attack(Character &c)
{
c.hitpoints - = 20;
}
现在,当您从 main()
函数调用 main.attack(monster) 时,monster 的 hitpoints
将减少 20。
作为旁注,将 class 的数据成员设为私有被认为是一种很好的做法,以避免数据的非法 access/modification。始终将成员函数用作 class 个实例的接口。