抽象类和静态方法

abstract class and static methods

本文关键字:静态方法 抽象类      更新时间:2023-10-16

我有一个抽象类:

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();
}  

实际上::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_;
}

我想分解this::instance方法,但是考虑到它使用虚拟方法::load,我不能在类A中定义它。从理论上讲,我知道这很奇怪,因为类A应该有0个实例,B,C应该有1个实例,但这段代码应该被分解也是有意义的。

你怎么解决这个问题?

你可以让instance()成为一个自由的函数模板:

template<class T>
T& instance()
{
  static T instance_;
  if (!instance_.loaded_)
  {
    instance_.load();
    instance_.loaded_=true;
  }
  return instance_;
}

那么你可以这样使用:

instance<B>().do_stuff()

这是CRTP的一种常见用法,定义在模板中创建实例的函数,然后在每种类型中实例化它:

struct A {
   virtual ~A() {}     // don't forget to make your destructor virtual!
   virtual void load() = 0;
};
template <typename T>
struct instance_provider {
    static T& instance() { /* implementation */ }
};
struct B : A, instance_provider<B> {
   virtual void load();
};
struct C : A, instance_provider<C> {
   virtual void load();
};