必须通过价值 [英] must pass by value

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

问题描述

我的文本提供了大量示例,其中通过引用传递

必要。例如,着名的交换示例。


但是,我从未见过按值传递变量的示例

是必要的。


我推荐的值得推荐的地方是

抛出异常。但是,我的文章从未说过为什么通过

引用引用是错误的。 (当然,很多人已经解释了为什么

追逐价值是不好的做法。)假设我确实通过引用投掷

using namespace std;并包括正确的异常库。

我可以写一下:


throw& runtime_error(" Incorrect ... blah blah");


或者这段代码是不正确的?我的想法是使用字符串Incorrect ... blah blah初始化runtime_error

。并抛出

引用该错误。


任何人都可以给出一个链接或给出一个例子来说明
$ b的技巧为了使函数正常工作,需要$ b传递值,并且

通过引用传递会导致问题。


我看到有时会通过两个理由按价值(不确定这是否是
正确)。 1)对于简单的内置类型来说,按值传递更快

就像int一样。 2)按价值传递更容易编码(虽然更大的

轻松可能只会对初学者产生影响。)


这似乎不够该语言的原因包括

传值的概念。


是否有理由不按价值传递所有内容?

感谢您的解释。


Paul Epstein

My texts give plenty of examples where passing by reference is
necessary. For instance, the famous swap example.

However, I''ve never seen an example where passing a variable by value
is necessary.

One place where I''ve seen passing by value recommended is in the
throwing of exceptions. However, my texts never say why throwing by
reference is wrong. (Of course, lots of people have explained why
catching by value is poor practice.) Suppose I did throw by reference
using namespace std; and including the correct exceptions libraries.
Could I then write:

throw &runtime_error("Incorrect...blah blah ");

Or would this code be incorrect? My thinking is that a runtime_error
is initialized with the string "Incorrect...blah blah " and that a
reference to that error is thrown.

Can anyone give a link to or give an example where the technique of
passing by value is necessary to make a function work, and where
passing by reference would lead to problems.

I see two reasons to sometimes pass by value (not sure if this is
correct). 1) Passing by value is quicker for simple built-in types
like int. 2) Passing by value is easier to code (though the greater
ease probably only makes a difference to beginners.)

This doesn''t seem enough of a reason for the language to include the
passing-by-value concept.

Are there reasons not to pass everything by value?
Thank you for your explanations.

Paul Epstein

推荐答案

I假设一个缺点就是你不能同时

修改输入当它是一个参考...这是没有问题大多数

次,但我想有些情况你可能不想要的地方

必须锁定传递的值,直到调用者完成它。


只是一个想法
pa ********** @ att.net 写道:
I suppose one disadvantage would be that you could not concurrently
modify the input when it is a reference... this is no problem most
times, but I suppose there are situations where you might not want to
have to lock the passed value until the caller finishes with it.

just a thought
pa**********@att.net wrote:

我的文字提供了大量的例子,其中通过引用传递

必需品元。例如,着名的交换示例。


但是,我从未见过按值传递变量的示例

是必要的。


我推荐的值得推荐的地方是

抛出异常。但是,我的文章从未说过为什么通过

引用引用是错误的。 (当然,很多人已经解释了为什么

追逐价值是不好的做法。)假设我确实通过引用投掷

using namespace std;并包括正确的异常库。

我可以写一下:


throw& runtime_error(" Incorrect ... blah blah");


或者这段代码是不正确的?我的想法是使用字符串Incorrect ... blah blah初始化runtime_error

。并抛出

引用该错误。


任何人都可以给出一个链接或给出一个例子来说明
$ b的技巧为了使函数正常工作,需要$ b传递值,并且

通过引用传递会导致问题。


我看到有时会通过两个理由按价值(不确定这是否是
正确)。 1)对于简单的内置类型来说,按值传递更快

就像int一样。 2)按价值传递更容易编码(虽然更大的

轻松可能只会对初学者产生影响。)


这似乎不够该语言的原因包括

传值的概念。


是否有理由不按价值传递所有内容?


感谢您的解释。


Paul Epstein
My texts give plenty of examples where passing by reference is
necessary. For instance, the famous swap example.

However, I''ve never seen an example where passing a variable by value
is necessary.

One place where I''ve seen passing by value recommended is in the
throwing of exceptions. However, my texts never say why throwing by
reference is wrong. (Of course, lots of people have explained why
catching by value is poor practice.) Suppose I did throw by reference
using namespace std; and including the correct exceptions libraries.
Could I then write:

throw &runtime_error("Incorrect...blah blah ");

Or would this code be incorrect? My thinking is that a runtime_error
is initialized with the string "Incorrect...blah blah " and that a
reference to that error is thrown.

Can anyone give a link to or give an example where the technique of
passing by value is necessary to make a function work, and where
passing by reference would lead to problems.

I see two reasons to sometimes pass by value (not sure if this is
correct). 1) Passing by value is quicker for simple built-in types
like int. 2) Passing by value is easier to code (though the greater
ease probably only makes a difference to beginners.)

This doesn''t seem enough of a reason for the language to include the
passing-by-value concept.

Are there reasons not to pass everything by value?
Thank you for your explanations.

Paul Epstein


pa ********** @ att.net 写道:

我的文本提供了大量示例,其中通过引用传递

必要。例如,着名的交换示例。


然而,我从未见过一个通过

值传递变量的例子

是必要的。
My texts give plenty of examples where passing by reference is
necessary. For instance, the famous swap example.

However, I''ve never seen an example where passing a variable by
value
is necessary.



来自C语言,其中传递值是常态,并且

引用是不可用的。

That comes from the C language, where pass by value was the norm, and
references isn''t available.


>

我推荐的值得推荐的地方是

抛出异常。但是,我的文章从未说过为什么通过

引用引用是错误的。 (当然,很多人已经解释了为什么

追逐价值是不好的做法。)假设我确实扔了

参考

使用命名空间性病;并包括正确的异常库。

我可以写一下:


throw& runtime_error(" Incorrect ... blah blah");


或者这段代码是不正确的?我的想法是使用字符串Incorrect ... blah blah初始化

runtime_error

并抛出了一个

引用该错误。
>
One place where I''ve seen passing by value recommended is in the
throwing of exceptions. However, my texts never say why throwing by
reference is wrong. (Of course, lots of people have explained why
catching by value is poor practice.) Suppose I did throw by
reference
using namespace std; and including the correct exceptions libraries.
Could I then write:

throw &runtime_error("Incorrect...blah blah ");

Or would this code be incorrect? My thinking is that a
runtime_error
is initialized with the string "Incorrect...blah blah " and that a
reference to that error is thrown.



不知道真的。有什么好处?投掷逻辑

无论如何都要经常复制,以确保在远处捕获点可以获得例外$
$

Don''t know really. What would be the advantage? The throwing logic
will often have to make a copy anyway, to ensure that the exception is
available at a far away catch point.


>

任何人都可以给出一个链接或给出一个例子,其中通过值传递

的技术是使函数工作所必需的,以及

通过引用传递会导致问题。
>
Can anyone give a link to or give an example where the technique of
passing by value is necessary to make a function work, and where
passing by reference would lead to problems.



按值调用的一个优点是函数获得自己的副本

参数。这确保了没有其他人同时在

上摆弄它。它也可以用作

函数中的本地工作空间。

One advantage of call by value is that the function gets its own copy
of the parameter. That ensures that nobody else is fiddling with it at
the same time. It can also be used as a local work space inside the
function.


>

我明白了有时通过价值的两个原因(不确定这是否是正确的b / b
)。 1)对于简单的内置类型来说,按值传递更快

就像int一样。
>
I see two reasons to sometimes pass by value (not sure if this is
correct). 1) Passing by value is quicker for simple built-in types
like int.



不是很多。拥有自己的副本可能会使函数机器代码

更简单。

Not by much. Having your own copy may make the functions machine code
simpler though.


2)按值传递更容易编码(尽管更大

轻松可能只对初学者产生影响。)
2) Passing by value is easier to code (though the greater
ease probably only makes a difference to beginners.)



按值传递并通过const引用看起来几乎是

相同,无论是呼叫站点还是功能内部。

Passing by value and passing by const reference looks pretty much the
same, both att the call site and inside the fucntion.


>

这似乎不够该语言包含

传值值概念的原因。
>
This doesn''t seem enough of a reason for the language to include the
passing-by-value concept.



对于C ++,它继承自C,它是唯一的。

For C++ it is inherited from C, where it was the only one.


>

是否有理由不通过值传递所有内容?
>
Are there reasons not to pass everything by value?



有时您想更新参数,可以使用

非常规参考。

Bo Persson

Sometimes you want to update the parameter, which can be done with a
non-const reference.
Bo Persson



re ******** @ yahoo.com 写道:

我认为一个劣势可能是你不能同时

修改输入时它是一个参考...这是没问题的大多数

次,但我想有些情况你可能会不希望

必须锁定传递的值,直到调用者完成它。


只是一个想法
I suppose one disadvantage would be that you could not concurrently
modify the input when it is a reference... this is no problem most
times, but I suppose there are situations where you might not want to
have to lock the passed value until the caller finishes with it.

just a thought



要么我误解了你,要么你错了。 (我很高兴认为我的理解是错误的。)


我编码并成功运行了以下程序:


#include< iostream>

使用命名空间std;


int main()

{

int y = 1;

int f(int&);

cout<< f(y);

cin.get();


}


int f(int& x)

{x = x + 2;

返回x;}


因此我似乎修改了输入。 (虽然我不知道

我是否同时修改它。)


x = x + 2用参考调用f修改输入( int& x)所以看来

(对我来说)我的帖子与你的帖子相矛盾。


谢谢,


Paul

Either I misunderstand you or you are wrong. (I''m happy to assume my
poor understanding is at fault.)

I coded and ran the following program successfully:

#include <iostream>
using namespace std;

int main()
{
int y = 1;
int f(int&);
cout << f(y);
cin.get();

}

int f(int &x)
{ x = x +2;
return x;}

I therefore appear to have modified the input. (Though I don''t know
whether I "concurrently" modified it.)

x = x+2 modifies input with the reference call f(int &x) so it appears
(to me) that I have contradicted your posting.

Thanks,

Paul


这篇关于必须通过价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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