函数不更改传递的指针C+。我有我的职责,我正在填补targetBubble在那里,但它不是在调用这个函数之后填充的,但是我知道它是在这个函数中填充的,因为我有输出代码。bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];}我像这样传递指针Bubble * targetBubble = NULL;clickOnBubble(mousePos, bubbles, targetBubble);为什么它不起作用?谢谢
3 回答
小唯快跑啊
TA贡献1863条经验 获得超2个赞
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];}
郎朗坤
TA贡献1921条经验 获得超9个赞
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)
慕桂英546537
TA贡献1848条经验 获得超10个赞
int b = 0;foo(b);int foo(int a){
a = 1;}int b = 0;foo(&b);int foo(int *a){
*a = 1;}int* b = 0;foo(b);int foo(int* a){
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to}int* b = 0;foo(&b);int foo(int** a){
*a = 1; // here you changing what b is pointing to}- 3 回答
- 0 关注
- 386 浏览
添加回答
举报
0/150
提交
取消
