一个函数原型是什么意思与和号在它? [英] What does a function prototype mean with an ampersand in it?

查看:158
本文介绍了一个函数原型是什么意思与和号在它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是'&'


在我的CS类中,老师向我们展示了一些函数和模板的一些示例以及函数的一些原型在参数列表中具有&符号,如下所示:

  void exchange & x,T& y); // prototype 

这是什么意思?

参考,用于参考强>。总之,这是一个像指针,不能是NULL 。维基百科有关于此主题的内容这里



当它们用于函数/方法调用时,引用是便宜,因为对象不需要在函数调用中复制。你仍然有相同的语法,如果你有一个复制的对象。使用指针,您将需要处理特殊情况,指针 NULL



这是使用它们的常见原因。如果我认为对,交换意味着像交换拖动对象x和y ,函数调用的成本直接与应对对象的成本相关,因此保存一些副本可能是相关的优化。


Possible Duplicate:
What does the '&' operator do in C++?

In my CS class today the teacher showed us some examples of functions and templates and some of the prototypes for functions had ampersands in the list of parameters like this:

void exchange( T & x, T & y ) ; // prototype

what does that mean? What should I use it for?

解决方案

the & is for reference. In short that's something like a pointer, that can't be NULL. Wikipedia has something on this topic here.

References are cheap when they are used in function/method calls, since the object doesn't need to be copied in your function call. You still have the same syntax as if you had with a copied object. With pointers you would need to handle the special case, that the pointer is NULL.

That is a usual reason to use them. If I guess right and exchange means something like swap the tow objects x and y, the the cost of the function call is directly related to the cost of coping the object, so saving some copies may be a relevant optimization.

这篇关于一个函数原型是什么意思与和号在它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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