C ++单例与完全静态对象 [英] C++ singleton vs completely static object

查看:135
本文介绍了C ++单例与完全静态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们需要在我们的项目中只有一个类的一个实例。有几种方法可以做。



我想比较。



2)完全静态类(所有方法和成员是静态的)。





根据我的理解,区别如下:



a)未定义跨不同单元的静态成员的初始化顺序。
所以,完全静态成员初始化不能使用来自其他模块的任何静态成员/函数。单例没有这个问题。



b)我们必须处理单线程的getInstance()的线程。但是,完全静态类没有这个问题。



c)访问方法看起来有点不同。
Foo :: bar(); vs Foo :: getInstance() - > bar();
通常,singleton可以返回NULL来标识对象的构造存在一些问题,静态类不能。



d)类的定义看起来



我错过了什么吗?

解决方案

无论你称之为Singleton还是Monostate或任何奇特的名字...这个非常恼人的性质是,对象的实例和许多写入:全局变量,无论他们的猜测是邪恶



唯一的实例通常很笨重。大多数时候,你真正需要的是通信的部分共享同一个实例。



任何声称需要全局变量的代码都是高度可疑的。使用它可能看起来更简单,但让我们面对它,你可以完美地将对象传递给每个函数,它会使它们的签名复杂化,但它仍然可以工作。



但是,我承认,使用全局变量似乎更简单,直到你注意到这些问题:




  • multithreading 受到损害

  • 可测试性会减少,因为一个测试可能会影响后面的测试
  • 复杂:当你从子方法中拉入全局方法时,很难知道你的方法所依赖的状态...


作为单例,多线程创建不可用于C ++在C ++ 0x之前(当它可能使用静态局部变量),因此你需要创建它在一个线程和延迟访问之前:实例化它在主,它是你最好的



销毁可能会导致伤害,因为Singleton / Static的生命可能会在其他人完成它之前结束,然后是未定义的行为。这是典型的 Logger 单例。通常的策略是无耻地泄漏...



然后,如果你还想要一个,祝你好运,这就是这个社区可以为你做的。 / p>

Let say we need to have just one instance of some class in our project. There are couple ways of doing it.

I want to compare. Please can you review my understanding.

1) Classical Singleton pattern

2) Completely static class (all methods and members are static).


As I understand the differences are following:

a) The order of initialization of static members across different units isn't defined. So, completely static members initialization can't use any static members/functions from other modules. And singleton doesn't have this problem.

b) We have to deal with threading for getInstance() of Singleton. However, completely static class doesn't have this problem.

c) Access to methods looks a little bit different. Foo::bar(); vs Foo::getInstance()->bar(); Generally, singleton can return NULL to identify that there were some problems with construction of object and static class can't.

d) Definition of class looks a little bit clunky with bunch of statics for static class.

Have I missed anything?

解决方案

Whether you call it Singleton or Monostate or any fancy name... the very annoying nature of this is that you have ONE instance of the object and many writes to it: global variables, whatever their guise, are evil.

The idea that you need a unique instance is generally clunky. Most of the times what you really need is parts that communicate share the same instance. But another group of parts could perfectly use another instance without issue.

Any code that claim to need a global variable is highly suspicious. It may appear simpler to use one, but let's face it, you could perfectly pass the object to each and every function, it would complicate their signature but it would work nonetheless.

However, I admit, it appears simpler to use global variables... until you notice the issues:

  • multithreading is compromised
  • testability is reduced, since one test may affect the one following it
  • dependency analysis is extremely complicated: it's hard to know what state your method depend on when you pull in global from within submethods...

Now, as far as singleton is concerned, multithreaded creation is not usable in C++ before C++0x (when it becomes possible using static locals), thus you need to create it in only one thread and delay access before: instantiate it in main, it's your best bet.

Destruction may cause mayhem since the life of the Singleton / Static may end before others are done with it, and then it's undefined behavior. This is typical of a Logger singleton. The usual strategy is to shamelessly leak...

After that, if you still want one, I wish you good luck, that's all this community can do for you.

这篇关于C ++单例与完全静态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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