*& 有什么用?函数参数中的平均值

2023-09-27C/C++开发问题
1

本文介绍了*& 有什么用?函数参数中的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果我有一个接受 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.

你可以只传递一个指针,一个复杂的问题是指针需要是一个左值,而不仅仅是一个右值,例如

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);

如果函数希望在给定 NULL 指针时分配内存,也可能有意义.检查随函数提供的文档(或阅读源代码!)以了解如何使用指针.

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.

这篇关于*& 有什么用?函数参数中的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6