引用和指针的含义在一起?

Sar*_*san 2 c++ pass-by-reference pass-by-pointer

在我的项目中,有一个像这样的函数调用的定义.

    int32 Map(void * &pMemoryPointer)
Run Code Online (Sandbox Code Playgroud)

在调用的地方,传递的参数是无效*,为什么我们只能将它作为指针本身接收,而不是这个?

Som*_*ude 6

在不知道Map函数的作用的情况下,我猜它会设置指针.因此必须通过引用传递.


使用对指针的引用,可以分配内存并将其分配给函数内的指针.例如

void DoSomething(int*& pointerReference)
{
    // Do some stuff...

    pointerReference = new int[someSize];

    // Do some other stuff...
}
Run Code Online (Sandbox Code Playgroud)

制作这样的函数的另一种方法是返回指针,但是当问题中的Map函数返回不能使用的其他函数时.