c++用另一个类的指针设置一个类的值
c++ set value of a class with a pointer of another class
我正试图实现游戏交易或不交易,我有两个类,盒子&;的球员。在box中存储向量game_box,在Player中我想存储玩家必须保留到游戏结束的盒子和钱。
我已经尝试过用这种方式实现它。它运行正确,没有错误,但是当我试图验证值是否存储到setter中时,它只是告诉我setter是空的。我真的不明白为什么!有人知道为什么吗?
class Box
{
vector<Box> game_box;
float pound_contained;
int box_number;
public:
Box(int box_number, float pound_contained);
Box();
int getbox_number();
float getpound_contained();
};
class Player :public Box
{
int player_box;
float player_money;
public:
Player();
~Player();
float getplayer_money();
int getplayer_box();
void setplayer_money(float);
void setplayer_box(int);
};
void Player::setplayer_money(float)
{
player_money = player_money;
}
void Player::setplayer_box(int)
{
player_box = player_box;
}
float Player::getplayer_money()
{
return this->player_money;
}
int Player::getplayer_box()
{
return this->player_box;
}
int main()
{
vector<Box> game_box;
srand(time(0));
int n;
Player money;
Box oper;
float myArray [22][2] = { { 0.01, 0 }, { 0.10, 0 }, { 0.50, 0 },
{ 1, 0 }, { 5, 0 }, { 10, 0 },
{ 50, 0 }, { 100, 0 }, { 250, 0 },
{ 500, 0 }, { 750, 0 }, { 1000, 0 },
{ 3000, 0 }, { 5000, 0 }, { 10000, 0 },
{ 15000, 0 }, { 20000, 0 }, { 35000, 0 },
{ 50000, 0 }, { 75000, 0 }, { 100000, 0 },
{ 250000, 0 }
};
//random assignation of pound value to the 22 boxes
for (int e = 1; e <22; e++)
{
int pos;
bool op = true;
while (op)
{
pos = rand() % 22 + 1;
if (myArray[pos][1] == 0)
{
myArray[pos][1] = 1;
op = false;
}
}
Box b(e, myArray[pos][0]); //creating the class game
game_box.push_back(b); //function of the vector to insert a data in it
}
// random assignment of a box to the player
int i = rand() % 22 + 1;
Box* boxes = &game_box[i];
cout << "Your box is going to be the box number: "<< boxes->getbox_number()<<endl;
////////////////////////////////////////////////////////////////////setter not working
money.setplayer_money(boxes->getpound_contained());
money.setplayer_box(oper.getbox_number());
cout << money.getplayer_box() << " " << money.getplayer_money() << endl << endl;
game_box.erase(game_box.begin()+i);
return 0;
}
c++中没有与其他方法调用不同的"setter "或"getter "。因此,您可以像编写任何其他方法一样编写它们。
为了详细说明@maniek指出的问题,你写道:
void Player::setplayer_money(float)
{
player_money = player_money;
}
C和c++可以指定不需要名称的方法和函数参数——只需要类型。这可能看起来有点奇怪,因为没有办法访问该参数(至少不是一种保证在所有编译器或优化级别中都能工作的方式,您可以通过搞乱堆栈来实现)。因此,您在这里所做的只是将成员player_money
设置为自身。
(注意:如果你想知道为什么它允许你指定一个没有名字的方法参数,它有几个用途…一个是,不命名它会抑制未使用该参数的警告消息。因此,它是一种标记当前不使用但仍然需要的东西的方法——可能是出于遗留原因,也可能是因为您将来可能会使用它。
可以为形参指定一个与成员变量名称不重叠的新名称:
void Player::setplayer_money(float new_player_money)
{
player_money = new_player_money;
}
这是避免歧义的一种方法。因为就作用域内的值而言,形参将胜过成员变量。这是另一个什么都不做的操作,它将参数值赋给自身:
void Player::setplayer_money(float player_money)
{
player_money = player_money;
}
(注意:由于player_money是按值传递的,而不是按引用传递的,这不会改变调用站点参数的值。特别是,如果传入一个像10.20
这样的常量,它如何改变值呢?)
@maniek建议的是,在这种情况下消除歧义的一种方法是当您表示成员变量时使用this->player_money
,当您表示方法的参数时使用player_money
。有些人做的另一件事是专门命名他们的成员变量——比如用m_
开始,就像m_player_money
:
void Player::setplayer_money(float player_money)
{
m_player_money = player_money;
}
(注意:只要下一个字符是小写的,你也可以只使用没有m
的下划线前缀,但有些人认为这太危险了,因为下划线后面跟着大写字母是为编译器内部使用保留的。)
作为最后一个想法—-如果职业名称是Player
,那么它已经暗示了你将设置谁的钱(玩家的),所以你可以将其称为set_money
。此外,我不喜欢在名字中使用下划线(在C中比c++更常见),所以我可能会称之为setMoney
。
如何:
void Player::setplayer_money(float player_money)
{
this->player_money = player_money;
}
?
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何设置一个范围来提取我想要获得的信息
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 将双精度变量设置为另一个变量的值
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- C++设置了一个用于排序的比较器和另一个用于唯一性的比较器
- 如何设置默认参数以防用户不输入另一个参数
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++如何将字符串数组中的一个点设置为枚举变量
- 给定一个整数 N>0,区间 [0, 2^N) 中有多少个整数正好有 N-1 个设置位?编写一个返回正确答案的简短函数
- 如何设置一个简单的CGAL+Qt程序
- 是否可以为一个循环设置多个条件
- 在C++中设置仅包含每个类的一个外观的数据结构
- 如果我有五个 Win32 静态控件,如何使用特定的前景色设置其中一个?
- 有人可以给我一个设置和检索unique_ptr的简单示例吗?
- 在c++中强制打开一个设置大小的终端窗口
- 在理解指针指向一个设置为NULL的对象时出现问题
- C++将两个变量中较大的一个设置为新值