多个单例实例 [英] Multiple Singleton Instances

查看:219
本文介绍了多个单例实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个实用程序类库,其中许多都是单例.我已经使用继承实现了它们:

I am writing a library of utility classes, many of which are singletons. I have implemented them as such using inheritance:

template <class T>
class Singleton {
    public:
        T& getInstance() {
            if(m_instance == 0) {
                 m_instance = new T;
            }

            return m_instance;
        }
    private:
        static T* m_instance;
};

class SomeClass : public Singleton<SomeClass> {
    public:
        SomeClass() {}
        virtual ~SomeClass() {}

        void doSomething() {;}
};

显然,这是一个简单的示例,而不是实际的类.无论如何,我发现使用以下代码来实现这一点:

Obviously this is a simple example, not an actual class. Anyways, I am finding that using code such as:

SomeClass::getInstance().doSomething();

将创建多个SomeClass实例.我认为这可能是由于它在我的库(.a)文件之外以及在内部使用.例如,我正在使用一个不是我自己编写的UI库,该UI库是单独编译的并且要添加其他内容.其中一些增加利用单例,我的.a库中也使用了单例.

Will create more than one instance of SomeClass. I am thinking this may be due to the fact that it is being used outside my library (.a) file as well as internally. For example, I am using a UI library not written by myself which is separately compiled and to which I am making additions. Some of these additions utilize singletons which are also being used in my .a library.

是由单独的编译引起的吗?还有吗?

Is the separate compilation causing this? Something else?

我设法解决该问题的唯一方法是在main.cpp文件中创建一个全局对象,该对象将用需要的任何单例进行初始化.然后,所有代码都通过以下调用来访问此公共全局对象:

The only way I have managed to get around the issue is to create a global object in my main.cpp file which I initialize with any singletons I will need. Then all code accesses this common global object with calls such as:

GlobalObject::getSomeClass().doSomething()

我讨厌每次创建另一个单例时都必须向该对象添加其他方法.另外,使用第一种访问方法的语法似乎更清晰,更熟悉:

I hate having to add an additional method to this object every time I create another singleton. Plus the syntax seems clearer and more familiar using the first access method:

SomeClass::getInstance().doSomething();

如果您有任何想法,意见等,请告诉我.

Please let me know if you have any thoughts, opinions, etc.

谢谢.

推荐答案

您的问题是,由于模板是完全内联的,因此将在多个编译单元中实例化您的模板.因此,在使用该模板的每个编译单元中,最终将创建一个单例(每个编译单元).您需要强制全局链接,以便所有编译单元都引用相同的模板实例化.即将发布的C ++标准将通过外部模板对此提供支持.现在,您可以执行的操作是在项目中禁用自动实例化,并手动实例化您显式使用的模板.这样,当您在任何编译单元中使用模板时,您都会生成对该实现的未知引用,然后链接程序可以从(一个)编译单元(在其中进行显式实例化)满足该实现.

Your problem is that your template is going to be instantiated in more than one compilation unit as it is completely inline. Therefore in every compilation unit that uses the template you will end up creating one singleton (per compilation unit). What you would need is to force global linkage, so that all compilation units reference the same template instantiation. The upcoming C++ standard will support this via extern template. What you can do now is to disable automatic instantiation in your project and manually instantiate the templates that you use explicitly. This way when you use the template in any compilation unit you will generate an unknown reference to the implementation which can then be satisfied by the linker from the (one) compilation unit where you do the explicit instantiation.

这篇关于多个单例实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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