是全局变量不好? [英] Are global variables bad?

查看:124
本文介绍了是全局变量不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C / C ++,是全局变量一样糟糕,我的教授认为他们是谁?

In C/C++, are global variables as bad as my professor thinks they are?

推荐答案

全局变量的问题是,因为每一个功能都有访问这些,它变得越来越难以找出哪些功能实际上读写这些变量。

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.

要了解应用程序是如何工作的,你pretty多有考虑到每一个它修改全局状态的功能。这是可以做到的,但随着应用的增长将得到更难被几乎是不可能的(或者至少是完全是浪费时间)的地步。

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.

这篇关于是全局变量不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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