c++类设计,“虚拟选项结构和设置器”?

发布于 2024-11-18 10:52:48 字数 718 浏览 2 评论 0原文

我不确定要在谷歌上搜索或搜索什么,这是否是“虚拟成员变量”或类似的东西。我有类似下面的内容:

class ConcreteClass: public AbstractBaseclass {
public:
  struct Options {
     int x;
     string foo;
  };
  int options(int ac, char ** av, Options& opts) {
  // Declare the supported options.
  po::options_description desc("Allowed options");
  // ... etc, fill in with boost program options 
  }

  virtual void  MandatoryFunction1( ... ) { } 

  }

不同类型的“ConcreteClass”将具有完全不同的内容...从其他地方继承选项似乎很愚蠢,因为它必须是一个空的基本结构。

是否有一种智能方法通过“AbstractBaseclass”强制派生类必须声明一些“Options”结构,并提供一个函数“options()”来填充它?这只是为了强制与派生类中使用核心库的方式保持一定的一致性。

*编辑:如果我需要编辑基类来支持这一点,这是可以的。我想它可能包含一个纯虚拟的 options() 函数,但我不确定让它接受待定义的“Options”结构的好方法是什么。

I'm not sure what to google or search for on this one, would this be a 'virtual member variable' or something of that sort. I have something like the below:

class ConcreteClass: public AbstractBaseclass {
public:
  struct Options {
     int x;
     string foo;
  };
  int options(int ac, char ** av, Options& opts) {
  // Declare the supported options.
  po::options_description desc("Allowed options");
  // ... etc, fill in with boost program options 
  }

  virtual void  MandatoryFunction1( ... ) { } 

  }

Different types of 'ConcreteClass' will have radically varying things in them ... seems silly to inherit Options from somewhere else as it would have to be an empty base struct.

Is there an intelligent way to mandate, via the 'AbstractBaseclass', that the derived class must declare some "Options" struct, and provide a function "options()" to fill it in? This is just to force some consistency with how the core library is being used in derived classes.

*Edit: it is OK if I need to edit the base class to support this. I'm thinking maybe have it contain a pure-virtual options() function, but I'm not sure what a good way is to have it accept a to-be-defined "Options" struct.

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

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

发布评论

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

评论(2

过期以后 2024-11-25 10:52:48

我见过一些这样的我如何强迫人们扩展我的课程类型的问题。真正的问题是为什么你要强迫它?需要他们这样做还是你希望他们这样做?

如果您需要它们实现该功能,则意味着代码的某些部分实际上会使用它,如果它们不实现它,编译器将触发错误并无法编译,并且您的问题无需执行任何操作即可解决。

如果您只是希望他们这样做,请重新考虑您的策略。强迫别人的代码做不需要的事情是没有意义的。也许您的类的某些扩展根本不需要选项,为什么当他们不需要它们并且您也不需要它们时必须来实现该功能?

请注意,很少会找到依赖于任何实现的X::Options结构的代码(模板除外),因为您将无法使用该代码一般 --C++ 使用静态类型。因此,您很可能不使用该代码一般,或者您需要一个Options接口。如果您在模板中使用它,则代码将无法编译模板,您甚至可以在模板中计算出如何生成简单易读的错误消息(想想static_assert),如果您不使用它,问题是:为什么

I have seen a couple of this how do I force people extending my class to do X type of questions. The real question there is why do you want to force it? Do you need them to do it or you just want them to?

If you need them to implement that functionality, it means that some part of the code will actually use it, and if they don't implement it, the compiler will trigger an error and fail to compile, and your problem is solved without having to do anything.

If you just want them to, rethink your strategy. There is no point in forcing other's code to do something that is not needed. Maybe some extension to your class does not need options at all, why would they have to implement that function when they don't need them and you don't need them either?

Note that it is uncommon to find code (the exception is with templates) that depends on any X::Options struct implemented, as you will not be able to use that code generically --C++ uses static typing. So chances are that either you are not using that code generically or you need an Options interface. If you are using that in a template, the code will fail to compile the template, and you can even work out in the template how to produce a simple to read error message (think static_assert), if you are not using it the question stands: why?

千と千尋 2024-11-25 10:52:48

只需遵循 2 个简单的步骤,您就会得到您想要的:

(1) 将 AbstractBaseclass 中的 options() 方法设为纯 virtual,这样它将强制每个子class实现它:

(2) 在AbstractBaseclass下的private说明符内创建struct Options。因此,现在每个子class都必须有自己的Options来满足virtual options()参数。

因此,总体而言,您的基将如下所示:

class AbstractBaseclass {
  struct Options {};  // private
public:  // pure virtual 
  virtual int options(int ac, char ** av, Options& opts) = 0;
};

Just follow 2 simple steps and you will get what you want:

(1) Make options() method pure virtual in AbstractBaseclass, so it will mandate every child class to implement it:

(2) Make struct Options inside private specifier under AbstractBaseclass. So, now every child class has to have its own Options to satisfy the virtual options() argument.

So overall your base class will look like:

class AbstractBaseclass {
  struct Options {};  // private
public:  // pure virtual 
  virtual int options(int ac, char ** av, Options& opts) = 0;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文