对singleton类的多文件访问
multi file access to singleton class
我有一个singleton类。
在A.h 中
class single
{
public:
static single *Instance;
static single* getInstance()
{ if(!Instance) Instance = new single;
return Instance;
}
void hello () { cout<<"Hello"; }
private: single(){ }
}
在A.cpp 中
single *single::Instance = 0;
std::auto_ptr <single> SINGLE_OBJ (single::getInstance());
在B.cpp 中
#include "A.h"
SINGLE_OBJ->hello();
我得到以下错误:未在此作用域中声明SINGLE_OBJ。
要使SINGLE_OBJ
在B.cpp
中可见,您应该在A.h.
中声明它,即:
extern std::auto_ptr <single> SINGLE_OBJ;
此外,为什么要使用std::auto_ptr
,它已被弃用-您应该切换到std::unique_ptr
。
相关文章:
- 防止临时对象文件访问 MSVC 中的磁盘
- 内存映射文件访问非常慢
- 从多个文件访问静态全局 WinForm UI 对象
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- 如何在Android中从本机编译的c ++二进制文件访问相机
- 如何:监视Windows中另一个进程的文件访问?
- 如何从NTFS中的$ MFT文件访问文件的每个块的文件指针
- 执行C 代码时快速频繁的文件访问
- 使用TDD时隐藏文件访问实现详细信息
- 从C文件访问C 类成员功能
- 从另一个CPP文件访问Form1的文本框,Visual C 2010
- 在所有CPP文件中使用一个Globe变量.如果CPP文件的一个类更改该值,我想从另一个类CPP文件访问它
- 如何从C 文件访问$(target_arch)
- 系统访问出界异常,尝试从flatbuffer的二进制文件访问"LengthofTable"
- 如何声明可通过多个头文件访问的对象
- 使用二进制文件访问违规读取位置
- 哪种类型的文件访问使用
- 在其他源文件中没有通用的头文件访问功能
- 文件访问不起作用
- Windows 8 应用商店应用程序C++文件访问