C++ 如何创建灵活的模板C++;

C++ 如何创建灵活的模板C++;,c++,templates,inheritance,C++,Templates,Inheritance,我试图创建一个抽象类,它是另一个类的模板。是否可以创建“灵活的”模板 有几个类将继承自这个类,所有这些类都将具有相同名称但参数不同的函数。抽象类是继承类的“接口”——我将使用这个类的指针来管理另一个类 例如,我们有两个类:A和B。 find类的A方法只需要type1类型,但是B类的相同方法需要type1和type2类型 以下是我创建从模板继承的类的方式: class A : public Repository<int> { public void find(int) overr

我试图创建一个抽象类,它是另一个类的模板。是否可以创建“灵活的”
模板

有几个类将继承自这个类,所有这些类都将具有相同名称但参数不同的函数。抽象类是继承类的“接口”——我将使用这个类的指针来管理另一个类

例如,我们有两个类:
A
B
find
类的
A
方法只需要
type1
类型,但是
B
类的相同方法需要
type1
type2
类型

以下是我创建从模板继承的类的方式:

class A : public Repository<int> {
   public void find(int) override; };


class B : public Repository<int, float> {
   public void find(int a, float b) override; };

您需要在基类中使用可变模板,即

#include <iostream>

template <typename ... Args>
class Interface
{
  public:
    virtual void find(Args... args) = 0;
};

class Impl1 : public Interface<int>
{
  public:
    void find(int value) override
    {
      std::cout << "found" << value << std::endl;
    }
};

class Impl2 : public Interface<int, float>
{
  public:
    void find(int value, float other_value) override
    {
      std::cout << "found" << value << " " << other_value << std::endl;
    }
};

int main()
{
  Impl1 impl1 {};
  impl1.find(5);

  Impl2 impl2 {};
  impl2.find(5, 10.2);
}
#包括
模板
类接口
{
公众:
虚拟空查找(Args…Args)=0;
};
类Impl1:公共接口
{
公众:
无效查找(int值)覆盖
{

std::cout为了补充@KKMKK的以下注释,以下是如何从Args获取特定类型…(from:):

模板
类接口
{
公众:
使用FirstType=typename std::tuple\u元素::type;
虚拟空添加(FirstType)=0;
虚拟空查找(Args…Args)=0;
};
类Impl2:公共接口
{
公众:
无效添加(int值)覆盖
{

这听起来像是老式重载的例子,不需要templates@pm100我必须使用模板。因此,您希望编写
class A:public Repository
,并检测
A::find(int)
以推断应该使用
Repository
“我必须使用模板”-为什么?@KKMKK这正是
Args…
给您带来的。它的意思是“零,一个或多个参数”。请参阅参数包和变量模板。此解决方案提供了另一个问题。如果我的模板有多个方法,例如:
虚拟类型1添加(类型1,类型2),该怎么办
?如果我正确理解您的意思,那么将该方法添加到派生实现中,并使用两种类型。派生实现可以覆盖
find(type1)
add(type1,type2)
@AdvSphere好的,但是如何添加
参数的
接口
方法?
#include <iostream>

template <typename ... Args>
class Interface
{
  public:
    virtual void find(Args... args) = 0;
};

class Impl1 : public Interface<int>
{
  public:
    void find(int value) override
    {
      std::cout << "found" << value << std::endl;
    }
};

class Impl2 : public Interface<int, float>
{
  public:
    void find(int value, float other_value) override
    {
      std::cout << "found" << value << " " << other_value << std::endl;
    }
};

int main()
{
  Impl1 impl1 {};
  impl1.find(5);

  Impl2 impl2 {};
  impl2.find(5, 10.2);
}
template <typename ... Args>
class Interface
{
  public:
    using FirstType = typename std::tuple_element<0, std::tuple<Args...> >::type;

    virtual void add(FirstType) = 0;
    virtual void find(Args... args) = 0;
};

class Impl2 : public Interface<int, float>
{
  public:
    void add(int value) override
    {
      std::cout << "found" << value << std::endl;
    }
    void find(int value, float other_value) override
    {
      std::cout << "found" << value << " " << other_value << std::endl;
    }
};

int main()
{
  Impl2 impl2 {};
  impl2.add(5);
  impl2.find(5, 10.2);
}