模板类特定类型的功能

尤里·elburikh

好的,所以我有这个模板类,有点像单向列表。

template <typename T> List

它具有此内部功能打印

public:
void Print();

其中,因为你可以猜测,从王建宇,山西高等学校打印列表内容; 但是,由于模板可以将类视为T,因此可以想象,在这种情况下,我将需要Print()的不同实现。例如,我有另外一个类Point

class Point{
 private:
  int x, y;
 public:
  int getX();
  int getY();
}

所以我要专门为积分设计打印。我尝试了这个:

void List<Point>::Print();

但是编译器告诉我

prototype for void List<Point> Print() doesn match any in class List<Point>

虽然

candidates are: from List<T> [with T = Point] void List<Point>::Print()

对我来说,这似乎是一样的功能。怎么了?以及如何编写T特定的模板类函数?

戴着帽子的公鸡

您可以使用显式模板专门化来专门化Print特定类型的行为

例如,对于Point

template <> // No template arguments here !
void List<Point>::Print() // explicitly name what type to specialize
{
  //code for specific Point Print behaviour..
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章