指向抽象指针的函数重载 class

Function overload for pointer to abstract class

我在 C++ 中的函数重载方面遇到了一些问题。

我有一个 class 层次结构,其中许多 class 继承自抽象基础 class,如下所示:

struct Animal {
  virtual void make_noise() = 0;
};

struct Dog : Animal {
  void make_noise() { std::cout << "bark\n"; }
};

struct Cat : Animal {
  void make_noise() { std::cout << "meow\n"; }
};

struct Lion : Cat {
  void make_noise() { std::cout << "roar\n"; }
};

我想要一个根据参数类型具有三种不同实现的函数:

这是我的尝试:

void f(Animal *x) {
  x->make_noise();
}

void f(std::vector<Animal *> x) {
  std::cout << "vector\n";
}

template<class T>
void f(T a) {
  std::cout << a << "\n";
}

int main() {
  f(new Lion);
  std::vector<Animal *> x;
  f(x);
  f(2);
  return 0;
}

这是上面程序打印的内容:

0x7febb8d00000
vector
2

这是我想要打印的内容:

roar
vector
2

此外,如果我尝试传递 std::vector<Lion *> 而不是 std::vector<Animal *>,它会选择最后一个实现而不是第二个实现并生成编译器错误。

如何在 C++98 中解决这个问题?

一种方法是使用模板特化并在调用 f 时指定模板参数类型,如下所示:

template<class T>
void f(T a) {
  std::cout << a << "\n";
}

template<>
void f(Animal *x) {
  x->make_noise();
}

template<>
void f(std::vector<Animal *> x) {
  std::cout << "vector\n";
}


int main() {
  f<Animal *>(new Lion); // specify template param
  std::vector<Animal *> x;
  f(x);
  f(2);
  return 0;
}