函数参数类型后跟*& [英] Function argument type followed by *&

查看:118
本文介绍了函数参数类型后跟*&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些由别人写的代码,其中一些函数接受参数,其数据类型后跟一个*&我习惯于采取一个或另一个,例如。取double *或double&但不是两者。它会认为他们会取消。



这里有一个例子,这是从他们的代码,据说是工作原理:



一个函数声明为:

  void someClass :: foo(const unsigned int *& ubuff);然后在我的主文件中,有一个指向一些UINT的指针,并且初始化为:

  unsigned int * pbuff = new UINT [n]; 

然后,someClass :: foo函数被调用为:

  foo(pbuff); 



当编译时,我得到错误无法将参数1从'unsigned int *'转换为' const unsigned int *&'。如果我将我的函数调用更改为以下,它编译正确:

  foo(const unsigned int *& )); 

所以,我的问题是:


  1. *& ;?的目的是什么?也就是说,与*或&他们自己,为什么他们不只是取消自己?这是什么使函数foo期望?地址? A值?地址的地址?


  2. 是将unsigned int *简单地转换为const unsigned int *&确定或者我需要做其他的事情吗?


只是另一个简单的例子,文件由别人分发,所以我认为这是为他们编译的。他们有一个函数调用

  klabels = new int [sz]; 
EnforceLabelConnectivity(klabels,m_width,m_height,nlabels,numlabels,double(sz)/ double(STEP * STEP));

当我尝试构建时,我收到一个错误'SLIC :: EnforceLabelConnectivity':无法转换参数1从'int *'到'const int *&'。



感谢一如既往。

解决方案

好吧,我试过转换到一个(const unsigned int *),并没有工作。最后,我不得不完全转换到一个(const unsigned int *&)在foo函数示例在我的原始帖子。希望这可以帮助。谢谢。



另一方面,我没有走我的主要代码中开始pbuf​​f const的原因是它被传递给另一个函数,修改它,所以我不认为它会工作,它使它成为const时声明。


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

Then in my main file, there's a pointer to some UINTs declared and initialized as:

unsigned int* pbuff = new UINT[n];

Then, the function someClass::foo is called as:

foo(pbuff);

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

So, my questions are:

  1. 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.

  2. Is the fix of simply casting a "unsigned int*" to a "const unsigned int*&" ok or do I need to do something else?

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

When I try to build, I get an error "'SLIC::EnforceLabelConnectivity' : cannot convert parameter 1 from 'int *' to 'const int *&'".

Thanks as always.

解决方案

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.

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.

这篇关于函数参数类型后跟*&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆