静态类成员在动态库中与主应用程序不同
Static class member are different within dynamic libraries from main application
我目前正在开发一个大量使用单例和动态库(使用dlopen)的应用程序。
我的问题是我的单例在应用程序和动态库中的实例是不同的(即在我的实现中,单例::getInstance方法返回不同的地址)
这就是我定义单例的方式
// Singleton.hpp
template<typename T>
class Singleton
{
static T *_instance;
protected:
Singleton<T>() {}
virtual ~Singleton<T>() {}
public:
static T *getInstance() {
if (_instance == NULL)
_instance = new T;
return (_instance);
}
static void deleteInstance() {
if (_instance) {
delete _instance;
_instance = NULL;
}
}
};
template<typename T>
T *Singleton<T>::_instance = NULL;
然后,如果我希望类测试是单例,我需要做:
// Test.hh
#include "Singleton.hpp"
class Test : public Singleton<Test>
{
friend class Singleton<Test>;
private:
Test();
public:
virtual ~Test();
void doSomeStuff();
};
现在,如果我想使用测试,我会:
#include "Test.hh"
int main() {
Test *test = Test::getInstance();
test->doSomeStuff();
return (0);
}
为了解决这个问题,我试图以某种方式"extern"(使用关键字)静态类成员 Singleton::_instance,但我从未成功编译我的应用程序......GCC拒绝这种代码(在Singleton.hpp中):
template<typename T>
extern T *Singleton<T>::_instance;
以下内容需要移出singleton.hpp。正如所写的那样,它为每个包含 singleton.hpp 的.cpp文件创建一个指针实例。
template<typename T>
T *Singleton<T>::_instance = NULL;
请记住,这是一个定义,而不是一个声明。因此,包含标头的每个对象文件现在都在定义 Singleton::_instance。如果多个.cpp文件包含 singleton.h 并在单个目标(可执行文件或库)中实例化具有相同类型的模板,您将获得多重定义的符号。
若要使此代码正确,需要选取单个.cpp文件(在单一实例模板的客户端中)并在那里定义_instance。这显然不是非常可取的,因此请考虑不使用单例(您的第一个最佳选择)或使用不同的单例模式。
相关文章:
- 试图在visual studio上用C++创建一个桌面应用程序
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 在C应用程序中运行C++(带有STL)函数
- 使用VerQueryValue检索应用程序的文件描述
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用调试/崩溃报告将应用程序部署到客户端
- C++控制台应用程序阻止退出
- 码头化的C++应用程序是否向后兼容早期的内核版本
- 将应用程序从32位移植到64位时出现问题
- 如何改变c++应用程序的视觉效果
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 静态类成员在动态库中与主应用程序不同
- 在不减慢主应用程序速度的情况下在后端运行成员函数的技巧是什么
- 调用指向windows过程中C函数的函数的成员指针会导致应用程序崩溃
- 具有矢量成员的单例应用程序
- 卸载应用程序域不清除C++ COM 对象静态成员
- 使用库的c++应用程序说没有检测到成员