简体   繁体   English

具有继承的C ++模板专业化

[英]C++ Template Specialization with Inheritance

Following Situation: 以下情况:

class FeatureBase
class Feature1 : public FeatureBase

class FeatureAttrBase
class Feature1Attr : public FeatureAttrbase

FeatureBase contains a list of FeatureAttrBase and should be able to create and manage these objects. FeatureBase包含FeatureAttrBase的列表,并且应该能够创建和管理这些对象。 Therefore i use a template on FeatureBase. 因此,我在FeatureBase上使用模板。

template<class T = FeatureAttrBase> class FeatureBase

creating and managing the attributes (eg new T()) 创建和管理属性(例如新的T())

and the subclasses use a specialized inheritance 并且子类使用专门的继承

class Feature1 : public FeatureBase<Feature1Attr>

Anywhere else in my code i wrote a method 我在代码的其他地方写了一个方法

RegisterFeature(FeatureBase<FeatureAttrBase>* feature)

but the compiler gives me an error that it was unable to convert between Feature1 and FeatureBase. 但是编译器给我一个错误,它无法在Feature1和FeatureBase之间转换。 In the mentioned method i only need to use information from FeatureAttrBase. 在提到的方法中,我只需要使用来自FeatureAttrBase的信息。 But inside Feature1 i need access to Feature1Attr. 但是在Feature1内部,我需要访问Feature1Attr。

Thus the question is how to solve this issue? 因此问题是如何解决这个问题? Do i have to change my datastructure? 我是否需要更改数据结构?

Having template parameters inherit from each other doesn't make template classes related. 模板参数彼此继承不会使模板类相关。 You should instead do something like the following (might not be the best solution but you haven't specified what you are trying to do): 相反,您应该执行以下操作(可能不是最好的解决方案,但您尚未指定要执行的操作):

class FeatureAttrBase;
class FeatureBase
{
public:
    virtual FeatureAttrBase* GetAttributes() = 0;
};

template<class T>
class FeatureImpl : public FeatureBase
{
    T attr;
public:
    FeatureAttrBase* GetAttributes()
    {
        return &attr;
    }
};

class Feature1Attr : public FeatureAttrBase;
class Feature1 : public FeatureImpl<Feature1Attr>;

In fact, you probably don't need the FeatureImpl class and can put the implementation directly in the Feature1 class (and get rid of templates completely). 实际上,您可能不需要FeatureImpl类,并且可以将实现直接放在Feature1类中(并完全摆脱模板)。

You could inherit the specialisations of FeatureBase from FeatureBase<FeatureAttrBase> . 您可以从FeatureBase<FeatureAttrBase>继承FeatureBase的专业化。 Something like this: 像这样:

// Forward declaration
template <typename T>
class FeatureBase;

// Type selecting trait class FeatureBase_BaseClass
// FeatureBase for derived Attrs will inherit from FeatureBase<FeatureAttrBase>
template <typename T>
struct FeatureBase_BaseClass
{
  typedef FeatureBase<FeatureAttrBase> Type;
};

// FeatureBase<FeatureAttrBase> will inherit from a dummy type
template <>
struct FeatureBase_BaseClass<FeatureAttrBase>
{
  struct Type {};
};



// Use FeatureBase_BaseClass to select the proper base class for FeatureBase
template <typename T = FeatureAttrBase>
class FeatureBase : public FeatureBase_BaseClass<T>::Type
{
  //as before
};

This way, all FeatureBase<X> for specific attributes X will inherit from FeatureBase<FeatureAttrBase> . 这样,特定属性X所有FeatureBase<X>都将从FeatureBase<FeatureAttrBase>继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM