Function argument type followed by *amp;(函数参数类型后跟 *amp;)
问题描述
我有一些由其他人编写的代码,其中一些函数采用数据类型后跟 *& 的参数.我习惯于以一种或另一种方式发挥作用,例如采取双*"或双&"但不是两者兼而有之.它会认为他们会取消.
I have some code written by someone else in which some functions take arguments whose data types are followed by a *&. I'm used to functions taking one or the other, e.g. taking a "double *" or a "double &" but not both. It would have thought they would just cancel out.
这是一个例子,这完全来自他们应该可以工作的代码:
Here's an example, and this is all from their code which supposedly works:
在头文件中有一个函数声明为:
In a header file there's a function declared as:
void someClass::foo(const unsigned int*& ubuff);
然后在我的主文件中,有一个指向声明和初始化为的一些 UINT 的指针:
Then in my main file, there's a pointer to some UINTs declared and initialized as:
unsigned int* pbuff = new UINT[n];
然后,函数 someClass::foo 被调用为:
Then, the function someClass::foo is called as:
foo(pbuff);
当我编译时,出现错误无法将参数 1 从 'unsigned int *' 转换为 'const unsigned int *&'".如果我将函数调用更改为以下内容,则编译正常:
When I compile, I get the error "cannot convert parameter 1 from 'unsigned int *' to 'const unsigned int *&'". If I change my function call to the following, it compiles ok:
foo((const unsigned int *&)(pbuff));
所以,我的问题是:
*& 的目的是什么?即,它与 * 或 & 有何不同?他们自己,为什么他们不直接取消自己?这使函数 foo 期望什么?一个地址?一个值?地址的地址?令人困惑.
What is the purpose of the *&? I.e., how is that different from * or & by themselves, and why do they not just cancel themselves out? And what does that make the function foo expect? An address? A value? The address of an address? It's confusing.
是简单地将unsigned int*"转换为const unsigned int*&"的解决方法好的还是我需要做其他事情?
Is the fix of simply casting a "unsigned int*" to a "const unsigned int*&" ok or do I need to do something else?
只是另一个简单的例子,它完全来自其他人分发的 cpp 文件,所以我假设这是为他们编译的.他们有一个函数调用
Just another quick example, and this is from completely within a cpp file distributed by someone else, so I assume this compiled for them. They have a function call as
klabels = new int[sz];
EnforceLabelConnectivity(klabels, m_width, m_height, nlabels, numlabels, double(sz)/double(STEP*STEP));
当我尝试构建时,出现错误'SLIC::EnforceLabelConnectivity':无法将参数 1 从 'int *' 转换为 'const int *&'".
When I try to build, I get an error "'SLIC::EnforceLabelConnectivity' : cannot convert parameter 1 from 'int *' to 'const int *&'".
一如既往的感谢.
推荐答案
好吧,我尝试强制转换为 (const unsigned int *),但没有奏效(无法构建).最后,我不得不在原始帖子的 foo 函数示例中完全转换为 (const unsigned int *&) .希望这可以帮助.谢谢.
Well, I tried casting to a (const unsigned int *), and that did not work (wouldn't build). In the end, I had to fully cast to a (const unsigned int *&) in the foo function example in my original post. Hope this helps. Thanks.
顺便说一句,我没有在我的主代码中从一开始就使 pbuff const 的原因是它稍后被传递给另一个函数,后者确实修改了它,所以我认为它行不通在声明时让它成为 const .
As an aside, the reason I did not go the route of making pbuff const from the beginning in my main code is that it is passed to another function later on which does modify it, so I don't think it would work to have it made const when declared.
这篇关于函数参数类型后跟 *&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:函数参数类型后跟 *&


- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01