相关疑难解决方法(0)

Linux上的共享库中的多个单例实例

正如标题所提到的,我的问题很明显,我详细描述了这个场景.在文件singleton.h中有一个名为singleton的类,由singleton模式实现,如下所示:

/*
 * singleton.h
 *
 *  Created on: 2011-12-24
 *      Author: bourneli
 */

#ifndef SINGLETON_H_
#define SINGLETON_H_

class singleton
{
private:
    singleton() {num = -1;}
    static singleton* pInstance;
public:
    static singleton& instance()
    {
        if (NULL == pInstance)
        {
            pInstance = new singleton();
        }
        return *pInstance;
    }
public:
    int num;
};

singleton* singleton::pInstance = NULL;

#endif /* SINGLETON_H_ */
Run Code Online (Sandbox Code Playgroud)

然后,有一个名为hello.cpp的插件如下:

#include <iostream>
#include "singleton.h"

extern "C" void hello() {
    std::cout << "singleton.num in hello.so : " << singleton::instance().num << std::endl;
    ++singleton::instance().num; …
Run Code Online (Sandbox Code Playgroud)

c++ singleton dlopen

36
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×1

dlopen ×1

singleton ×1