模板并拆分为.h / .cpp文件

时间:2012-05-06 02:03:51

标签: c++ templates

  

可能重复:
  Why can templates only be implemented in the header file?
  Why should the implementation and the declaration of a template class be in the same header file?

我将创建几个ADT并比较它们的性能(我正在学习数据结构课程并希望进行实验;注意:我们的教师使用Java)。我想创建泛型类,以便我不会遇到特定的数据类型。我很久以前读过,不可能分割模板类。保持代码清洁的最佳方法是什么?最明显的方法是简单地使用.cpp文件而不隐藏实现,例如在java中。我还可以使用其他方法吗?

1 个答案:

答案 0 :(得分:5)

有些人使用标题中包含的“ipp”文件。

除此之外......它就是它的本质。您不能将模板代码放在已编译的文件中,并且可以在其外部使用它。