函数不改变传递的指针C ++ [英] Function does not change passed pointer C++

查看:105
本文介绍了函数不改变传递的指针C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的功能,我填充 targetBubble 在那里,但它不是填充后调用此函数,但我知道它是填充这个函数,因为我有输出代码。

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.

bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
    targetBubble = bubbles[i];
}

我传递的指针如下

Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);

为什么不工作?感谢

推荐答案

因为你正在传递一个指针的副本。要改变指针,你需要这样的:

Because you are passing a copy of pointer. To change the pointer you need something like this:

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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆