我只是在想,如果在編譯時可以使用新的C++ 11類內成員初始化程序來初始化單身人士,這可能是我的一些經理類的加速應用:在編譯時初始化單身人士
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標誌集]
這個類看起來不像一個單身... – jrok 2012-04-12 12:31:28
除了不完整的類型問題,你如何期望在編譯時執行'operator new'的調用? – Job 2012-04-12 12:39:47
@Job完全錯過了這一點。謝謝 – niktehpui 2012-04-12 12:48:17