[英]Singleton initialization at compile-time
我只是在考慮新的C ++ 11類內成員初始化器是否可以用於在編譯時初始化Singletons,這可能是我的應用程序中某些Manager-Classes的加速:
class CSingleton
{
public:
CSingleton(void) {}
~CSingleton(void) {}
static const CSingleton* GetInstance(void)
{
return Instance;
}
bool Foo1(int x);
bool Foo2(int y);
private:
static constexpr CSingleton *Instance = new CSingleton();
}
問題是導致以下錯誤:
Line of Instance declaration: error: invalid use of incomplete type 'class test::CSingleton'
First Line of class declaration: error: forward declaration of 'class test::CSingleton'
有沒有辦法在編譯時使用這種或另一種方法初始化單身人士?
[我在MacOSX10.7(和Ubuntu)上使用GCC4.7並設置-std = c ++ 0x flag]
在.h文件類成員:
static CSingleton s_Instance;
在包含后的開頭右邊的.cpp文件中
CSingleton::s_Instance = CSingleton();
這是編譯時的初始化。 使用new - 這是在運行時初始化。 正式地,它們都在編譯時初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.