什么是*&在函数参数中的平均值 [英] What does *& mean in a function parameter
问题描述
如果我有一个函数 int *&
,这是什么意思?我如何只传递一个int或指针int到该函数?
If I have a function that takes int *&
, what does it means? How can I pass just an int or a pointer int to that function?
function(int *& mynumber);
每当我尝试传递一个指向该函数的指针时:
Whenever I try to pass a pointer to that function it says:
error: no matching function for call to 'function(int *)'
note: candidate is 'function(int *&)'
推荐答案
到一个int。这意味着所讨论的函数可以修改指针以及int本身。
It's a reference to a pointer to an int. This means the function in question can modify the pointer as well as the int itself.
你可以传递一个指针,一个复杂性是指针需要一个l值,而不仅仅是一个r值,例如
You can just pass a pointer in, the one complication being that the pointer needs to be an l-value, not just an r-value, so for example
int myint;
function(&myint);
单独不足,也不允许0 / NULL,其中:
alone isn't sufficient and neither would 0/NULL be allowable, Where as:
int myint;
int *myintptr = &myint;
function(myintptr);
当函数返回时,很可能 myintptr
将不再指向它最初指向的对象。
would be acceptable. When the function returns it's quite possible that myintptr
would no longer point to what it was initially pointing to.
int *myintptr = NULL;
function(myintptr);
检查函数提供的文档(或阅读源代码!)以了解如何使用指针。
might also make sense if the function was expecting to allocate the memory when given a NULL pointer. Check the documentation provided with the function (or read the source!) to see how the pointer is expected to be used.
这篇关于什么是*&在函数参数中的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!