我意識到這已被討論了多少次,但我還沒有找到適合我的問題的解決方案。我剛剛在我的項目中實現了一個Meyer的單例類,但我想從中創建一個模板,以便我可以將它用作例如。模板化單例類 - 如何處理私人構造函數
class Game : public Singleton<Game> { /* stuff */ }
我有這樣定義的
template <typename T>
class Singleton
{
public:
static T& Instance();
private:
Singleton();
//declare them to prevent copies
Singleton(Singleton const&);
void operator=(Singleton const&);
};// END OF CLASS DEFINITION
// METHODS' DEFINITIONS
template<typename T>
T& Singleton<T>::Instance()
{
static T _instance;
return _instance;
}
允許男星我的課是public
會破壞單身的整個視野。
編輯 好了,我已經更新了我的Game
類交好Singleton<Game>
class Game : public Singleton<Game>
{
friend class Singleton<Game>;
//...
}
但現在我有這樣的事情:
未定義參考「辛格爾頓<遊戲> :: Singleton()'
在功能Game::Game()
這是空的
你的問題到底是什麼?由於Singleton-ctor是私人的,該遊戲無法調用Singleton的ctor? - >保護Singleton的ctor。還是說Game的玩家必須是公開的,以便Singleton :: instance可以構造遊戲對象? –
MadScientist
2012-07-13 09:18:36