2
我有一个抽象类:抽象类和静态方法
class A
{
public:
bool loaded_;
virtual int load() = 0;
}
和几个派生类:
class B:public A
{
public:
int load();
static B& instance();
}
class C:public A
{
public:
int load();
static C& instance();
}
事实是,里面的代码::实例()方法是相同的每个类:
static B& B::instance()
{
static B instance_;
if (!instance_.loaded_)
{
instance_.load();
instance_.loaded_=true;
}
return instance_;
}
static C& C::instance()
{
static C instance_;
if (!instance_.loaded_)
{
instance_.load();
instance_.loaded_=true;
}
return instance_;
}
我想分解此::实例方法,但假设它使用虚方法:: load,ic annot在A类中定义它。 从理论上讲,我知道它很奇怪,因为A类应该有0个实例,而B应该有1个实例,但是这个代码应该被分解也是有意义的。
你会如何解决这个问题?
看看使用te模板来做到这一点。 –