通过引用传递功能 [英] Pass by reference to functions

查看:60
本文介绍了通过引用传递功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的两个函数中询问,参数是通过引用获取值还是什么?两者有什么区别?

I meant to ask in both of the functions below, parameters are getting the values by reference or what? What''s the difference between the two?

int main()
{
int a,b;
Stack stack_numbers
cin>>a>>b;
call (&a,&b);
do_command(stored_numbers);
}


功能1


Function 1

call(int *a1,int *b1)
{
//whatever
}


功能2


Function 2

bool do_command(Stack &numbers)
{
//whatver
}

推荐答案

浏览链接:

http://www.oocities.com/sstutor/pointer2a.htm [ http://www.geekinterview.com/question_details/14458 [
Go through the links :

http://www.oocities.com/sstutor/pointer2a.htm[^]

http://www.geekinterview.com/question_details/14458[^]

I am sure after this you can answer the questions yourself!


您知道按值调用和按引用调用的概念吗?
请搜索google ...
Do you know the concept of Call by value and call by reference ??
Please search google...


在可能的情况下避免在函数原型中使用指针,使编译器无法进行许多优化.优先使用引用,因此至少您不需要检查空指针.传递指针的情况可能与使用void *或继承有关.

对于效率类,应该优先使用按引用传递或const引用,但是对于基本类型,按值传递是可以接受的,因为它们更可能进入寄存器.

请注意,按值传递会复制该结构,这就是为什么除了基本类型以外,它效率极低的原因.
Avoid pointer use in function prototypes when possible, renders compilers unable to make many optimizations. Prefer to use reference, so you at least do not need to check against null pointer. The cases where you would pass in a pointer might relate to the use of void * or inheritance.

Pass by reference or const reference should be preferred for classes for efficiency, but pass by value is acceptable for fundamental types since they are more likely to go into registers.

Note that pass by value makes a copy of the structure, that''s why it is highly inefficient except for fundamental types.


这篇关于通过引用传递功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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