Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有继承的C++模板特化_C++_Templates_Inheritance - Fatal编程技术网

具有继承的C++模板特化

具有继承的C++模板特化,c++,templates,inheritance,C++,Templates,Inheritance,以下情况: class FeatureBase class Feature1 : public FeatureBase class FeatureAttrBase class Feature1Attr : public FeatureAttrbase FeatureBase包含FeatureAttrBase的列表,应该能够创建和管理这些对象。因此,我在FeatureBase上使用了一个模板 template<class T = FeatureAttrBase> class Fea

以下情况:

class FeatureBase
class Feature1 : public FeatureBase

class FeatureAttrBase
class Feature1Attr : public FeatureAttrbase
FeatureBase包含FeatureAttrBase的列表,应该能够创建和管理这些对象。因此,我在FeatureBase上使用了一个模板

template<class T = FeatureAttrBase> class FeatureBase
创建和管理属性,例如新T

子类使用专门的继承

class Feature1 : public FeatureBase<Feature1Attr>
我在代码的其他地方写了一个方法

RegisterFeature(FeatureBase<FeatureAttrBase>* feature)
但是编译器给了我一个错误,它无法在Feature1和FeatureBase之间转换。在上述方法中,我只需要使用FeatureAttrBase中的信息。但在Feature1内部,我需要访问Feature1Attr


因此,问题是如何解决这个问题?我必须更改数据结构吗?

让模板参数相互继承不会使模板类相互关联。相反,您应该执行以下操作,这可能不是最佳解决方案,但您尚未指定要执行的操作:

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>;

事实上,您可能不需要FeatureImpl类,可以将实现直接放在Feature1类中,完全摆脱模板。

让模板参数相互继承并不会使模板类相互关联。相反,您应该执行以下操作,这可能不是最佳解决方案,但您尚未指定要执行的操作:

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>;

事实上,您可能不需要FeatureImpl类,可以将实现直接放在Feature1类中,完全摆脱模板。

您可以从FeatureBase继承FeatureBase的专业化。大概是这样的:

// 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
};

这样,特定属性X的所有FeatureBase都将从FeatureBase继承

您可以从FeatureBase继承FeatureBase的专长。大概是这样的:

// 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
};

这样,特定属性X的所有FeatureBase都将从FeatureBase继承

那么问题是什么呢?Feature1和FeatureBase不属于同一层次结构,它们是不相关的类。是否有可能更改层次结构模板、继承以上述方式使用这些类?那么问题是什么?Feature1和FeatureBase不属于同一层次结构,它们是不相关的类。是否有可能更改层次结构模板、继承以上述方式使用这些类?好的。这是我的规范:我有几个特性,每个特性都有一个与环境中某些对象相关的属性列表。所有属性都有一个共同的基本特性ATTBASE。因此,特殊属性Feature1Attr可以在功能内部更新。但是还有一种更通用的算法运行在所有特征上,并读取公共值来计算总体结果。分离特征和特征属性的原因是什么?无论哪种方式,我的解决方案都应该没有问题,您可以摆脱FeatureImpl类,因为它实际上什么都不做——只需在Feature1中存储Feature1Attr,并让它在GetAttributes.OK中返回指针。这是我的规范:我有几个特性,每个特性都有一个与环境中某些对象相关的属性列表。所有属性都有一个共同的基本特性ATTBASE。因此,特殊属性Feature1Attr可以在功能内部更新。但是还有一种更通用的算法运行在所有特征上,并读取公共值来计算总体结果。分离特征和特征属性的原因是什么?无论哪种方式,我的解决方案都应该是可以的,并且您可以摆脱FeatureImpl类,因为它实际上什么都不做——只需在Feature1中存储Feature1Attr,并让它在GetAttributes中返回指针。