为什么将数组作为"int*&name"传递?

Hag*_*dol 6 c++ arrays

我得到了一个(C++)代码,其中使用了数组

void fun(int *& name){...}
Run Code Online (Sandbox Code Playgroud)

但这背后的想法是什么?我想这意味着"一个引用数组"但是当你只是将一个指针传递给第一个很好的元素时,不是吗?那么这样做的动机是什么?

oxy*_*ene 5

该函数接收对指针的引用.这意味着该函数不仅可以修改int指向的函数name,而且还可以在函数调用中对指针本身进行更改.

例:

#include <iostream>

int* allocate()
{
    return new int();
}

void destroy(int*& ptr)
{
    delete ptr;
    ptr = NULL;
}

int
main(int argc, char *argv[])
{
    int* foo = allocate();

    std::cout << foo << std::endl;

    destroy(foo);

    std::cout << foo << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

0x82dc008
0
Run Code Online (Sandbox Code Playgroud)