关于通过价值的问题 [英] Question about passing by value

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

问题描述

我知道数组和结构不能通过值传入和传出

函数,因为它们可以任意大。我的问题是:为什么

类型允许按值传递?难道我的类型不是很大,因为

很好吗?

解决方案

Method Man写道:

我知道数组和结构不能通过值传入和传出函数,因为它们可以任意大。


你理解不正确,但只是部分理解。类的对象

类型_can_可以传递给函数并通过

值从函数返回。数组不能,因为它们不是对象。

我的问题是:为什么
类型允许按值传递?难道我的类型不是很大吗?




我不确定你的意思。


Victor


Method Man写道:

我明白数组和结构不能通过值传递给功能因为它们可以任意大,所以它们都可以。我的问题是:为什么
类型允许按值传递?难道我的类型不是很大吗?




简单的答案很久很久以前它是由人们设计C的数据将是通过引用传递而不是

值的
。如果这是最好的选择是开放的辩论,但是我们现在坚持使用它了。


在C ++中,决定所有类应该通过默认值是通过

值传递的,因为它们的设计意图类似于int,char等类型的内置

而非类似数组(注意这个与Java不同的是
,它们通过引用传递给它们。这也是一个

的决定,现在我们也必须忍受这个:)


Chris

<方法人写道:

我知道数组和结构不能通过值传入和传出函数,因为它们可以任意大。


这并不完全正确。结构_can_可以通过值传入和传出

函数,无论它们有多大。数组不能,但不是因为它们的潜在大小而不是b $ b,而是因为数组既不是可分配的也不是可复制构造的。数组的这种限制大多数是因为历史原因。
我的问题是:为什么
类型允许通过值传递?难道我的类型不是很大吗?




什么是你的类型?在大多数情况下,您的非标量类型也将是b $ b数组或结构(类)。与问题的第一部分

有什么不同?


-

祝你好运,

Andrey Tarasevich


I understand that arrays and structs can''t be passed by value into and out
of functions since they can be arbitrarily big. My question is: Why are
types allowed to be passed by value? Couldn''t my types be arbitraily big as
well?

解决方案

Method Man wrote:

I understand that arrays and structs can''t be passed by value into and out
of functions since they can be arbitrarily big.
You understand incorrectly, but only partially so. Objects of class
type _can_ be passed into functions and returned from functions by
value. Arrays cannot because they are not objects.
My question is: Why are
types allowed to be passed by value? Couldn''t my types be arbitraily big as
well?



I am not sure what you mean here.

Victor


Method Man wrote:

I understand that arrays and structs can''t be passed by value into and out
of functions since they can be arbitrarily big. My question is: Why are
types allowed to be passed by value? Couldn''t my types be arbitraily big as
well?



The simple answer is that long, long ago it was decided by the people
who designed C that arrays would be passed by reference instead of
value. If this was the best choice is open to debate, however we are
stuck with it now :)

In C++ it was decided that all classes should by default be passed by
value, as they were designed with the intention of being like the built
in types like int, char, etc rather than like arrays (note that this is
different to Java, where they are passed by reference). This was also a
decision which was made, and now we have to live with that too :)

Chris


Method Man wrote:

I understand that arrays and structs can''t be passed by value into and out
of functions since they can be arbitrarily big.
That''s not entirely true. Structs _can_ be passed by value into and out
of function, regardless of how big they are. Arrays cannot, but not
because of their potential size, but rather because arrays are neither
assignable nor copy-constructible. This limitation for arrays has mostly
historical reasons.
My question is: Why are
types allowed to be passed by value? Couldn''t my types be arbitraily big as
well?



What "your types"? Your non-scalar types in most cases will also be
"arrays or structs (classes)". How is that different from the first part
of the question?

--
Best regards,
Andrey Tarasevich


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

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