我意识到这已被讨论了多少次,但我还没有找到适合我的问题的解决方案。我刚刚在我的项目中实现了一个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