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

标签 c++ pointers setter

我正在尝试实现交易​​或不交易游戏,我有两个类,Box 和 Player。在 box 中存储 vector 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++ 能够在没有名称的情况下指定方法和函数参数——只是类型。这可能看起来有点奇怪,因为没有办法访问该参数(至少不是保证在所有编译器或优化级别上工作的方法,您可以通过 messing with the stack 来实现)。所以你在这里所做的只是将成员 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

关于c++使用另一个类的指针设置一个类的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25171757/

相关文章:

c++ - 使用 boost::multi precision 进行单元测试

c++命名空间使用和命名规则

c++ - 将指针作为内存地址传递并使其永久化

java - 为什么我不能将此值传递到我的操作中?

c++ - 当参数转换为目标类型时,复制构造函数省略以直接初始化

c++ - 替换 std::string 中字符的最快(也是最安全)方法

c++ - 打印出指针值

C动态分配struct

inheritance - Kotlin 覆盖成员集并获取

Java - 我是否应该在 Setters 中使用 new() ?