作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
主要有以下代码:
Gameboard gameboard(8, 5);
HumanPlayer hp1('A'), hp2('B');
HumanPlayer players[2];
players[0] = hp1;
players[1] = hp2;
gameboard.show();
//int p = player
int input, p = 0;
while (cin >> input)
{
switch (input)
{
case 1:
if (players[p].setcoin(gameboard, input))
.
.
.
在 HumanPlayer 类中,我调用了 gameboard 的函数并返回它
bool HumanPlayer::setcoin(Gameboard g, int row)
{
return g.setstone(name, row);
}
在 Gameboard 类中,我设置硬币(如果它已满,我返回 false)
bool Gameboard::setstone(char player, int row)
{
for (int y = height; y >= 0; y--)
{
//row-1 da das array bei 0 beginnt
if (elements[y][row-1] == '.')
{
elements[y][row-1] = player;
return true;
}
}
return false;
}
最佳答案
此函数通过复制来传递游戏板:
bool HumanPlayer::setcoin(Gameboard g, int row)
{
return g.setstone(name, row);
}
这意味着您在函数内对 g 所做的一切都不会影响您传递给它的原始值。在函数结束时,g 的拷贝被销毁。
如果您不想这样做,请使用:
bool HumanPlayer::setcoin(Gameboard& g, int row)
{
return g.setstone(name, row);
}
& 符号表示您要传递引用,这不会导致生成拷贝。
也许 this question可能对您有用。
关于c++ - 为什么它调用 Gameboard gameboard 的析构函数 if (players[p].setcoin(gameboard, input)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43491932/
我是一名优秀的程序员,十分优秀!