c++用另一个类的指针设置一个类的值

c++ set value of a class with a pointer of another class

本文关键字:一个 设置 另一个 指针 c++      更新时间:2023-10-16

我正试图实现游戏交易或不交易,我有两个类,盒子&;的球员。在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;
}

?