如何在C ++中编写包含静态方法的多态接口

问题描述

假设我们有一个简单的继承结构。两个类继承自同一接口:

接口接口

我将跳过BaSEOne和BaseTwo标头。

//interface.h
class Interface {
  static Interface* Handle();
  void Foo() = 0; // Interface class is an abstract one
};

//base_one.cc
Interface* Interface::Handle() {/*implementation*/}

//base_two.cc
Interface* Interface::Handle() {/*different implementation*/}

现在在我们的主文件中,我们调用Interface :: Handle();

//main.cc
#include "interface.h"

int main() {
   Interface* = Interface::Handle();
}

问题:

  1. BaSEOne何时提供方法Handle()的定义,BaseTwo何时提供方法的定义?我们不会将它们直接包含在main.cc中。
  2. BaSEOne和BaseTwo都可以包含在同一个编译单元中吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)