我有我的函数,我正在那里填充targetBubble
,但是调用这个函数后它没有被填充,但是我知道它是在这个函数中填充的,因为我有输出代码。
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
我正在像这样传递指针
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
为什么它不起作用?
最佳答案
因为您正在传递指针的拷贝。要更改指针,您需要这样的东西:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
或
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}
关于c++ - 函数不会更改传递的指针 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11842416/