Recently I've bumped into a realization/implementation of the Singleton design pattern for C++.
(最近,我碰到了C ++的Singleton设计模式的实现/实现。)
(它看起来像这样(我从现实生活的示例中采用了它):)
// a lot of methods are omitted here class Singleton { public: static Singleton* getInstance( ); ~Singleton( ); private: Singleton( ); static Singleton* instance; };
From this declaration I can deduce that the instance field is initiated on the heap.
(从该声明中,我可以推断出实例字段是在堆上初始化的。)
(这意味着存在内存分配。)
(对我来说,完全不清楚的是何时确切地将要释放内存?)
(还是有错误和内存泄漏?)
(似乎实现中存在问题。)
My main question is, how do I implement it in the right way?
(我的主要问题是,如何以正确的方式实施它?)
In 2008 I provided a C++98 implementation of the Singleton design pattern that is lazy-evaluated, guaranteed-destruction, not-technically-thread-safe:
(在2008年,我提供了Singleton设计模式的C ++ 98实现,该模式是惰性评估的,保证销毁的,技术上不是线程安全的:) Can any one provide me a sample of Singleton in c++?
(任何人都可以为我提供c ++中的Singleton示例吗?)
Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe .
(这是Singleton设计模式的更新的C ++ 11实现,该实现是延迟评估,正确销毁和线程安全的 。)
class S { public: static S& getInstance() { static S instance; // Guaranteed to be destroyed. // Instantiated on first use. return instance; } private: S() {} // Constructor? (the {} brackets) are needed here. // C++ 03 // ======== // Don't forget to declare these two. You want to make sure they // are unacceptable otherwise you may accidentally get copies of // your singleton appearing. S(S const&); // Don't Implement void operator=(S const&); // Don't implement // C++ 11 // ======= // We can use the better technique of deleting the methods // we don't want. public: S(S const&) = delete; void operator=(S const&) = delete; // Note: Scott Meyers mentions in his Effective Modern // C++ book, that deleted functions should generally // be public as it results in better error messages // due to the compilers behavior to check accessibility // before deleted status };
See this article about when to use a singleton: (not often)
(请参阅本文,了解何时使用单例:(不常使用)) Singleton: How should it be used
(Singleton:应如何使用)
See this two article about initialization order and how to cope:
(请参阅这两篇有关初始化顺序以及如何应对的文章:) Static variables initialisation order
(静态变量初始化顺序) Finding C++ static initialization order problems
(查找C ++静态初始化顺序问题)
See this article describing lifetimes:
(请参阅描述寿命的文章:) What is the lifetime of a static variable in a C++ function?
(C ++函数中静态变量的生存期是多少?)
See this article that discusses some threading implications to singletons:
(请参阅本文,讨论对单例的一些线程含义:) Singleton instance declared as static variable of GetInstance method, is it thread-safe?
(声明为GetInstance方法的静态变量的Singleton实例,它是线程安全的吗?)
See this article that explains why double checked locking will not work on C++:
(请参阅这篇文章,解释为什么双重检查锁定在C ++上不起作用:) What are all the common undefined behaviours that a C++ programmer should know about?
(C ++程序员应该知道哪些常见的未定义行为?) Dr Dobbs: C++ and The Perils of Double-Checked Locking: Part I
(Dobbs博士:C ++和双重检查锁定的风险:第一部分)
2.1m questions
2.1m answers
60 comments
57.0k users