c++ - 函数不改变传递的指针 C++

标签 c++ function pointers

我有我的函数,我正在那里填充 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/48636896/

相关文章:

c++ - 发送 URL 时 chrome 中的奇怪字符

c++ - std::atomic 库依赖项 (gcc 4.7.3)

c++ - 将 vector 的 vector 复制到一维数组中

javascript - 两个功能相互干扰

C - 无法将 1 个参数从 int 转换为 int[][4]

c++ - 指针逻辑

c - 最简单的链表创建和打印数据

java - 根据 "foreign"子类型采取具体行动,无开关、转换等。我们能以某种方式使用多态性吗?

无法从 C 中的函数返回结构

c - 如何通过指向字符串数组来保存输入?