具有多个模板专业派生类的泛型基类 [英] Generic base class with multiple template specialized derived classes

查看:97
本文介绍了具有多个模板专业派生类的泛型基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实现几乎是相同数量的类,唯一不同的是它们处理的数据的基础类型:

I have a finite amount of classes with the nearly-same implementation, the only different being the underlying type of data they manipulate:

class IntContainer
{
public:
    void setData(int data);
    int getData();
    int _data;
};

class BoolContainer
{
public:
    void setData(bool data);
    bool getData();
    bool _data;
};

class StringContainer
{
public:
    void setData(std::string data);
    std::string getData();
    std::string _data;
};

// Etc. You get the idea.

我想通过使用如下模板来减少这些类的代码重复:

I'd like to reduce the code duplication of these classes by using templates like so:

template<typename T>
class GenericContainer
{
public:
    void setData(T data);
    T getData();
    T _data;
};

专业化:

typedef GenericContainer<int> IntContainer;
typedef GenericContainer<bool> BoolContainer;
typedef GenericContainer<std::string> StringContainer;

这很好。 但是,我还想向这些专门的类中添加一个抽象基类,以便能够以通用方式(例如在集合中)对其进行操作。问题在于,该基类应该具有 getData setData 方法,以便即使不知道动态也可以调用它们

This works well. But I'd also like to add an abstract base class to these specialized classes to be able to manipulate them in a generic way (eg. in a collection). The problem is this base class should have the getData and setData methods to be able to call them even without knowing the dynamic type of the object manipulated.

我会用这样的东西来实现它:

I would implement it with something like this:

class Base
{
public:
    virtual void setData(??? data) = 0;
    virtual ??? getData() = 0;
};

// Modify GenericContainer's definition like so
template<typename T>
class GenericContainer : Base { ... }

并以某种方式使用它: / p>

And use it somehow like that:

int main(int argc, char const *argv[])
{
    IntContainer intc = IntContainer();
    intc.setData(42);
    std::cout << intc.getData() << std::endl;

    BoolContainer boolc = BoolContainer();
    boolc.setData(false);
    std::cout << boolc.getData() << std::endl;

    std::vector<Base> v;
    v.push_back(intf);
    v.push_back(boolf);

    for (std::vector<Base>::iterator it = v.begin() ; it != v.end(); ++it)
        std::cout << it->getData() << std::endl;

    return 0;
}

问题是我不知道怎么写 Base 方法是原型,因为类型是未知的(并且没关系,应该基于对象的动态类型在运行时调用派生类的实现)。

The problem is I don't know how to write the Base methods prototypes as the type is unknow (and does not matter, the derived class implementation should be called at runtime based on the dynamic type of the object).

TL; DR:如何在几个完全专门的模板化类上实现抽象基类?

TL;DR: How to implement an abstract base class over several fully specialized templated classes ?

推荐答案

问题是,如果允许这样做,编译器将不得不在基类中生成尽可能多的虚方法。模板子类(可能是无穷大)可能无法进行专门化。

The problem is, if this was allowed, the compiler would have to generate as many virtual methods in the base class as there are possible specializations of the template child class (ie. an infinity) which is not possible.

这篇关于具有多个模板专业派生类的泛型基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆