C++ 引用传递的参数的行为类似于副本
我有两个指针叫做C++ 引用传递的参数的行为类似于副本,c++,function,pointers,reference,copy,C++,Function,Pointers,Reference,Copy,我有两个指针叫做Character*current=nullptr和字符*target=nullptr 我通过一个函数来更改它们: void Position::Current(const Vector2f & mouse_position, Character *& current) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) {
Character*current=nullptr代码>和字符*target=nullptr代码>
我通过一个函数来更改它们:
void Position::Current(const Vector2f & mouse_position, Character *& current)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
if ((mouse_position.x >= 245 + i * 164 && mouse_position.x <= 370 + i * 164) &&
(mouse_position.y >= 56 + j * 201 && mouse_position.y <= 221 + j * 201))
{
current = &positioning[i][j];
cout << "Selected: " << current->name << endl;
cout << endl;
current->health -= 5; //it doesn't change anything
break;
}
}
}
}
Position
类只有一个对象
在main()
positionx.Add(骑士3,4);
位置X.添加(恶魔,2,4);
然后:
if ((event.type == Event::MouseButtonPressed) && (event.mouseButton.button == Mouse::Right))
{
Vector2i pos = Mouse::getPosition(window);
Vector2f position = (Vector2f)pos;
positionx.Current(position, current);
}
当前函数应该从所选角色获得5点生命值,但不会发生类似的情况
我哪里出错了
谢谢你的回答。问题解决了
我只需要做角色定位[4][5]
->角色*定位[4][5]
然后在点击某个角色后,它终于开始像原始对象一样工作。问题解决了
我只需要做角色定位[4][5]
->角色*定位[4][5]
然后在点击某个角色后,它最终开始表现得像一个原始对象
if ((event.type == Event::MouseButtonPressed) && (event.mouseButton.button == Mouse::Right))
{
Vector2i pos = Mouse::getPosition(window);
Vector2f position = (Vector2f)pos;
positionx.Current(position, current);
}