查询的类成员返回错误的值
Queried class member returns wrong value
我在我的一个DLL(我们称之为exporting
DLL)中创建了一个简单的RAII类,它监视我的应用程序中的配置恢复:
头文件
class __declspec(dllexport) CEmuConfigurationRestoreMonitor
{
public:
CEmuConfigurationRestoreMonitor()
{
m_restoreInProgress = true;
}
~CEmuConfigurationRestoreMonitor()
{
m_restoreInProgress = false;
}
static bool IsRestoreInProgress()
{
return m_restoreInProgress;
}
private:
static bool m_restoreInProgress;
};
源文件
bool CEmuConfigurationRestoreMonitor::m_restoreInProgress = false;
其想法是exporting
DLL中的恢复代码将在堆栈上实例化CEmuConfigurationRestoreMonitor
,当它在方法结束时超出范围时,该标志将关闭
问题是,我想使用IsRestoreInProgress()
从另一个DLL(比如importing
DLL)查询标志。这就是为什么我将__declspec(dllexport)
放在exporting
DLL的类声明中。
当我链接importing
DLL时,我得到了一个未解析的m_restoreInProgress
符号。因此,我在importing
DLL中的.cpp文件中添加了以下行,它修复了这个问题:
bool CEmuConfigurationRestoreMonitor::m_restoreInProgress = false;
我现在发现,即使m_restoreInProgress
设置为true
,当我从importing
DLL查询它时,它总是返回false
。
importing
DLL中的静态初始化是否以某种方式覆盖了exporting
DLL中的实际(当前)值?
您为每个DLL提供了自己的m_restoreInProgress副本。
您可以通过以下方式解决此问题:
- 未使用内联函数
- 在仅包含在导出DLL中的源文件中,为m_resotreInProgress使用文件范围的变量
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- C++中函数的向量返回类型引发错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- glad 导致 glfwSwapBuffers 返回错误消息
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- QNX Momentics: GoogleMock - 返回有价值,在函数中返回 void 错误
- 我的模板类方法返回错误类型?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 替换WCHAR_T错误返回存储地址
- 错误:void 值未被忽略,因为它应该被忽略,错误:返回语句具有值
- 带有值的递归阶乘错误返回语句,在函数中返回 'void' [-fallowive]
- 存储在静态常量整数 (C++) 中的对数函数的错误返回值
- 更丰富的错误返回消息用于提升::精神::qi 解析
- C++初级逻辑错误-返回0
- 由于进程很多,pthread_create的错误返回代码是 35 错误,我用过pthread_exit应该杀死线程不是吗?
- c++错误处理返回值错误返回
- systememerror:没有异常集的错误返回