C ++中静态和全局的替代方法?

C ++中静态和全局的替代方法?

问题描述:

我有一个类实例,需要被一些其他类访问。​​

I have a class instance that needs to be accessed by some other classes.


  • 这是非常繁琐的传递实例总是

  • 我试图避免全局变量,因为人们倾向于反对这个。

  • 我认为我声明这个实例是静态的成员,然后包括此类为了访问实例,但这不工作

错误:调用类'Foo'的私有构造函数

在QGraphicsView框架的上下文中进一步指定问题:
我想添加QGraphicsItems通过一个控制器类(管理项)实例化到QGraphicsScene,这是(但我不坚持这个细节)我的QMainWindow类的成员。

To specify the problem further in the context of the QGraphicsView Framework: I want to add QGraphicsItems which are instantiated by a controller class (managing the items) to the QGraphicsScene, which is (but I do not insist on that detail) a member of my QMainWindow class.

我花了大量的时间在互联网上搜索,但我是一个相当新的,有点困在这里。

I spend a considerable amount of time searching the internet but I am fairly new and am kind of stuck here. I appreciate any incentive on what the best way to solve the dilemma would be.

使用全局变量是极具争议的讨论,这就是为什么我要指出,我在下面写的是我的个人意见,并开放讨论。

The use of globals are extremely controversy discussed, that's why I want to point out, that what I write in the following is my personal opinion and open to discussion.

我知道没有办法旁路传递实例来解决你的问题没有全局对象。我个人不认为全局实例在所有情况下都是邪恶的事情。

I know no way beside passing instance to resolve your problem without a global object. I personal do not think that a global instance is an evil thing in all situation.


  1. 当我设计一个库时,我倾向于避免全局对象,除了一个工厂。全局对象通常用作不需要快捷方式的快捷方式,或避免程序员进行一些打字。此外,大多数库中的类应该是独立的。

  1. When I design a library I tend to avoid global objects except one factory. Global objects are often used as shortcut where no shortcut is needed or to avoid some typing for the programmer. Also, mostly classes in a library should be independent. You sacrifice independency if you use global instances inside such classes.

当我在类中使用静态时,我只使用它们,如果整个类是一个单例工厂方法(例如创建或获取)。从来没有以你描述的方式。在你描述它的方式,这是意想不到的其他开发人员,因为你需要一个第三方来初始化该静态对象。除了避免键入更长的构造函数之外,您什么也不会得到。这不能是一个目标。

When I use statics in a class, I only use them if the whole class is a singleton or for factory methods (e.g. create or get). Never in a way you described. In the way you described it, it is unexpected for other developer, because you need a third party to initialize that static object. You gain nothing with that except avoid typing a longer constructor. This cannot be a goal.

如果我使用全局实例,我总是将其包装在管理类中。我从来没有使用像QGraphicsItems直接的第三方类作为全局变量(也没有原始类或类型)。像在第2点,其他人可能不会期望作为全球。更重要的是不清楚谁必须填充或杀死实例。 GraphicsItemManager可以有一个设置方法,使第三用户完全清楚。

If I use a global instance, I always wrap it in a management class. I never use third party classes like QGraphicsItems direct as globals (also no "primitive" classes or types). Like in point 2, other may not expect that as global. More important it is not clear who has to fill or kill the instance. A "GraphicsItemManager" can have a "setup" method which makes that totally clear to 3rd users.

传递实例不是在每种情况下最好的方式。不是所有的类都是重用的,它们只用于一个项目。这里的实现速度,易用性和明确的分组价值比一个教条没有全球化。大多数时候,我写经理类,它将例如图形项目。

Passing instances is not in every situation the best way IMHO. Not all my classes are for reuse, they are solely for one project. Here the speed of realization, the ease of use and the clear grouping worth more than a dogma as "no globals". Most time I write manager classes which group instances of e.g. graphic items. I do not have the feeling that makes the code less readable as long I used the rules I pointed out before.

有时资源在构建时不可用,所以你已经传递一个包装。这导致一个点,你有传递实例通过类,它们自己不需要该资源或者是库的自己的一部分(在这种情况下,通常你不能改变构造函数)。这可以(因为我们都是人类)导致误解。我使用全局管理类跳过那些gabs,因为我认为它更值得保持这个空间清洁依赖。

Sometimes a resource is not available by construction time, so you have already pass a wrapper. This leads to a point where you have pass instances through classes which themselves do not need that resource or are themself part of a library (in that case, often you cannot change the constructor). This can (as we are all humans) lead to misinterpretation. I use global management classes to jump over those "gabs", as I think it worth more to keep that space clean from dependencies.

正如我写的IMHO。