Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

c++ - C ++ Singleton设计模式(C++ Singleton design pattern)

Recently I've bumped into a realization/implementation of the Singleton design pattern for C++.

(最近,我碰到了C ++的Singleton设计模式的实现/实现。)

It has looked like this (I have adopted it from the real life example):

(它看起来像这样(我从现实生活的示例中采用了它):)

// 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.

(从该声明中,我可以推断出实例字段是在堆上初始化的。)

That means there is a memory allocation.

(这意味着存在内存分配。)

What is completely unclear for me is when exactly the memory is going to be deallocated?

(对我来说,完全不清楚的是何时确切地将要释放内存?)

Or is there a bug and memory leak?

(还是有错误和内存泄漏?)

It seems like there is a problem in the implementation.

(似乎实现中存在问题。)

My main question is, how do I implement it in the right way?

(我的主要问题是,如何以正确的方式实施它?)

  ask by Artem Barger translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ++和双重检查锁定的风险:第一部分)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...