【发布时间】:2013-02-09 22:24:32
【问题描述】:
在典型的单例中,第一次调用 getInstance() 时会调用构造函数。我需要的是分离 init 和 getInstance 函数。 init 函数必须使用构造函数 创建实例,并且只有在调用了 init 函数时才能使用 getInstance(否则它会抛出异常)。我该怎么做?
Singleton::init(required argument); //calls constructor
Singleton::getInstance(); //only possible if init had been called, otherwise throws exception
【问题讨论】:
-
不要使用单例并解决问题。
-
你有什么问题?
-
呃-哦。您真正要解决的根本问题是什么。单例模式可能不是您的答案。
-
好吧,经过一番思考,我认为我可能不会使用单例。
标签: c++ design-patterns constructor singleton