加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

当我们可以选择将变量设为外部时,为什么我们通过引用传递?

发布时间:2020-12-16 06:46:45 所属栏目:百科 来源:网络整理
导读:假设我们有一个数组说: int arr[1000]; 我有一个适用于该数组的函数说: void Func(void); 当我可以将arr [1000]作为main()外部的外部变量时,为什么需要通过引用传递(通过更改void)? 有什么区别?有什么区别吗? 为什么人们更喜欢通过引用传递而不是将其作
假设我们有一个数组说:

int arr[1000];

我有一个适用于该数组的函数说:

void Func(void);

当我可以将arr [1000]作为main()外部的外部变量时,为什么需要通过引用传递(通过更改void)?

>有什么区别?有什么区别吗?
>为什么人们更喜欢通过引用传递而不是将其作为外部传递? (我自己认为将它制作成外部更容易).

解决方法

我想你问的是 global variables are bad.引用 an excellent answer:

The problem with global variables is
that since every function has access
to these,it becomes increasingly hard
to figure out which functions actually
read and write these variables.

To understand how the application
works,you pretty much have to take
into account every function which
modifies the global state. That can be
done,but as the application grows it
will get harder to the point of being
virtually impossible (or at least a
complete waste of time).

If you don’t rely on global variables,you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does,as you don’t need to take the global state into account.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读