抽象类和静态方法

发布于 2024-11-26 03:57:18 字数 828 浏览 0 评论 0原文

我有一个抽象类:

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_;
}

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

你会如何解决这个问题?

I have an abstract class:

class A
{
  public:
  bool loaded_;
  virtual int load() = 0;
}

And several derived classes :

class B:public A
{
  public:
  int load();
  static B& instance();
}  

class C:public A
{
  public:
  int load();
  static C& instance();
}  

The fact is that the code inside ::instance() methods is the same for each class :

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_;
}

I would like to factorize this ::instance method, but given that it uses the virtual method ::load, i cannot define it in the class A.
Theoretically, i know it's weird since the class A should have 0 instance and B,C should have 1 instance but it also makes sense that this code should be factorized.

How would you solve that problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-12-03 03:57:18

您可以使 instance() 成为一个免费的函数模板:

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

然后您可以像这样使用它:

instance<B>().do_stuff()

You could make instance() a free function template:

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

Then you can use it like this:

instance<B>().do_stuff()
简单气质女生网名 2024-12-03 03:57:18

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

This is a common usage of the CRTP, define the function that creates the instance in the template and then instantiate it in each type:

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();
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文