指向不同 类 的函数指针向量

Vector of function pointers to different classes

我有一个函数指针的类型定义:

typedef int(interface1::*getInfoInterface1_t)(void);

然后是这些函数指针的向量:

std::vector<getInfoInterface1_t>

我想重用我创建的 class,但每个实例都实现了不同的虚拟接口 class。如果 class 不匹配,则指针将不匹配。

由于我无法弄清楚这一点,不幸的是我复制了这个 class,除了 typedef 和 vector 之外完全相同。

如果能有不同的函数签名就好了。

有办法解决这个问题吗?

只需制作一个 class 模板,其中 class 您要保持指针的模板参数是:

template  <typename Cls>
class Foo {
    typedef int(Cls::*getInfoInterface1_t)(void);
    std::vector<getInfoInterface1_t> ifcs;
    // ...
};

这样,对于每个 class 类型,您只需选择正确的模板:

Foo<interface1> your_original_version;