值参数和参考参数的区别? [英] Difference between value parameter and reference parameter?

查看:32
本文介绍了值参数和参考参数的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值参数和参考参数的区别?这个问题是面试官在我面试的时候问的.有人能告诉我用例子很容易解释的确切区别吗?并且引用参数和指针参数是一回事吗?

Difference between value parameter and reference parameter ? This question is asked sometime by interviewers during my interviews. Can someone tell me the exact difference that is easy to explain with example? And is reference parameter and pointer parameter are same thing ?

谢谢

推荐答案

对值参数的更改对调用者不可见(也称为按值传递").

Changes to a value parameter are not visible to the caller (also called "pass by value").

调用者可以看到对引用参数的更改(通过引用传递").

Changes to a reference parameter are visible to the caller ("pass by reference").

C++ 示例:

void by_value(int n) { n = 42; }
void by_ref(int& n) { n = 42; }

void also_value(int const& n); // Even though a reference is used, this is
// semantically a value parameter---though there are implementation
// artifacts, like not being able to write "n = 42" (it's const) and object
// identity (&n here has different ramifications than for by_value above).

指针的一种用途是在不使用特殊引用概念的情况下实现引用"参数,某些语言(例如 C)没有这种概念.(当然你也可以把指针当作值本身.)

One use of pointers is to implement "reference" parameters without using a special reference concept, which some languages, such as C, don't have. (Of course you can also treat pointers as values themselves.)

这篇关于值参数和参考参数的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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