C++ 模板与继承

标签 c++ templates inheritance code-complexity

我只是意识到提问是多么困难......希望我能举出足够精确的例子来说明我的问题,并且足够简短不会把一切都搞砸......至少有编辑的可能性。

这就是我目前的情况。当然,我在逻辑/结构(以及命名方面)方面做了一些修改,试图专注于我的问题的本质:

// MyClass deals with lists (actually several data structures) of the
// type MyType which should support different types and has to be 
// efficiently dealt with. Templating seems just right here
class MyClass
{
  ...

  void doSomething<class MyType>(vector<MyType> someList);

  ...

  // At some point I have to extract elements of the type MyType.
  // The extractor obviously depends on MyType but it is not possible to
  // Create a general version that could use templates itself 
  // (unless I use a specialization for each possible MyType)

  // I am stuck between these two alternatives:

  // Possibility1:
  // Let the client pass the right extractor and template it.
  template<class Extractor, class MyType>
  void extract(const Extractor& extractor, const string& source, 
               vector<MyType>* dest)
  {
     extractor.extract(source, dest);
  }

  // Possibility2:
  // Use a member _extractor of some base type that has to be set
  // to a specialization. The ExtractorBase has a virtual method
  // template<T> void extract(const string& source, vector<T>* myType) = 0
  // with no definition that is only defined in subclasses wrt certain
  // postings.
  ExtractorBase _extractor;

  template<class MyType>
  void extract(const string& source, vector<MyType>* dest)
  {
     _extractor.extract(source, dest);
  }
}

目前我更喜欢可能性 1,因为我不必在 Extractor 中为我想在未来尝试的所有 MyType 变体和关联的 Extractor 的继承搞乱。

另一方面,提取器可能需要复杂的代码和多个成员(类似于将特定输入映射到特定值的巨大 map )。所以使用模板不会带来性能提升。特别是仅使用头文件提取器,甚至可能是应该内联的仿函数,都是不可能的。过去,这一直强烈地提醒我,模板只会增加代码的复杂性(必须处理实例化,使客户端代码的工作更加困难等),我应该尽量避免它。

或者还有第三种我根本没想到的可能?

最佳答案

最好选择第一个选项。它更清洁且易于维护。

因为根据您的评论,我发现您对第二个选项的假设是错误的:

// The ExtractorBase has a virtual method
// template<T> void extract(const string& source, vector<T>* myType) = 0;

。那是不可能的; template 函数永远不能是virtual。因此,要实现第二个选项,您必须选择一些肮脏且难以维护的方法,这不是一个好主意。

关于C++ 模板与继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6593658/

相关文章:

c++ - 供应商提供的 C++ 库如何支持多个编译器?

c++ - 在另一个模板类构造函数中使用 typedef 模板时出错

具有条件支持的 Javascript 模板引擎

java - 使用包含数据的新 Array 或 ArrayList 调用父类(super class)的构造函数?

asp.net-mvc - 转换为派生类在 View 页面(aspx)上不起作用

c++ - 如何像 OpenGL 中的 glRotatef() 一样旋转顶点?

c++ - 零参数函数模板显式实例化似乎不起作用

c++ - 依赖于模板参数的成员变量和构造函数

c++ - 大小数组作为 C++ 中的函数参数类型

c# - 继承/泛型基础知识。如何实现 DRY 的代码结构