如何正确避免使用全局变量? [英] How to properly avoid using global variables?

查看:148
本文介绍了如何正确避免使用全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的课程

I have some classes like this

class A
{
public:
    B *b;
    C *c;
};

class B
{
};

class C
{
};



我有一些全局变量,用于A,B和C类。我知道使用全局变量不适合维护,所以我正在考虑转换为局部变量。我正在考虑这个


I have some global variables, that is used in A, B and C class. I know using global variables is unsuitable for maintenance, so I'm considering converting to local variables. I'm thinking about this

class C;

class B
{
    C *c;
    int local1;
};

class C
{
    B *b;
    int local2;
};



然后在A班,我做这样的参考


Then in A class, I make the reference like this

b = new B();
c = new C();
c->b = b;
b->c = c;



所以在B类中,a可以访问 local2 变量C喜欢这个


So in B class a can access local2 variable of C like this

void B::DoSomething()
{
    c->local2 = 0;
}



同样,我可以从C访问 local1 变量。这段代码有效,但我'我想知道,有更好的方法吗?我的意思是,一个明确的方法是交叉使用这些变量而没有这个交叉引用?

当然,local1 varialbe是属于B,local2是属于C的使用均值,所以我不想在一个类中声明 local1 local2


Similarly, I can access local1 variable from C. This code works, but I'm wondering, is there a better method? I mean, a clearly method to "cross use" these variables without this cross reference?
Of course, the local1 varialbe is "belong to" B, local2 is "belong to" C in its using mean, so I don't want to declare both local1, local2 in one class.

推荐答案

你的班级结构有点令人费解。在我看来,int值实际上可能属于A,然后B和C实例引用A的实例(在构造函数中)。 [A中的值应该是私有的'get'访问者。]



如果不是这样,你真的需要所描述的结构,C构造函数应该接受对B实例的引用,而B应该为其内部值设置一个set访问器。 B和C的内部值都应具有私有(或可能/可论证保护)访问权限并具有set / get访问器。您不应允许公共访问这些并直接从A访问它们。
Your class structure is a little puzzling. It seems to me that the int value(s) really might belong in A and then B and C instances refer to an instance of A (in the constructor). [The value(s) in A should then be private with a 'get' accessor.]

If this is not the case and you really need the structure as described, the C constructor should perhaps accept a reference to a B instance, and B should have a 'set' accessor for its internal value. Both B's and C's internal values should have private (or maybe/arguably protected) access and have set/get accessors. You shouldn't allow public access to these and access them directly from A.


变量所属的决策不应基于易于访问,而应基于实际关联。您没有指定这些局部变量的用途,以及它们描述的属性,但避免全局变量的核心是实现几乎每个变量都描述某种对象的某种属性或状态,并且因此应该与该对象绑定。 (如果你还没有,你应该为那个对象创建一个类)



可访问性实际上只是一个事后的想法。例如,如果您有一个描述B属性的变量,并且您需要为A和C中的特定操作访问此信息,那么将该属性作为函数参数传递,或者提供一个方法来读取此值属性,如果A / C已经可以访问B对象。



上述规则当然有例外。但是,如果您考虑某个变量描述的内容,它确实会有所帮助。 OTOH,如果不能轻易描述该局部变量的目的,这可能是由于变量重用;这完全是另一个问题,应该避免。有一个最近有关为什么不应该回收局部变量的文章 [ ^ ],它也适用于全局变量。也许更是如此!
The decision where a variable belongs shouldn't be based on "ease of access", but on actual association. You didn't specify what these local variables are for, and what property they describe, but at the heart of avoiding global variables is the realization that almost every variable describes some kind of property or state of some kind of object, and therefore should be tied to that object. (and you should create a class for that object if you didn't already)

Accessibility is really only an afterthought. If for instance you have a variable that describes a property of B, and you need to access this information for specific operations in A and C, then pass that property as a function parameter, or provide a method for be to read the value of this property, if A/C already have access to the B object.

There are certainly exceptions to the abovementioned 'rule'. But it really helps if you consider what, exactly, a certain variable describes. OTOH, if the purpose of that local variable cannot be described easily, this may be due to variable reuse; that is another problem entirely, and should be avoided. There is a recent article about why you shouldn't recycle local variables[^], and it applies to global variables as well. Maybe even more so!


我建​​议创建一个单独的类来保存变量。

可以使用方法而不是直接访问变量。 br />


您还可以使变量和方法保持静态,以避免必须创建类的对象。
I would recommend creating a separate class to hold the variables.
Access to the variables could be given using methods instead of directly.

You could also make the variables and methods static to avoid having to create an object of the class.


这篇关于如何正确避免使用全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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