类树中间的接口声明 (C++)

Declaration of interface in middle of classes tree (C++)

本文关键字:C++ 声明 接口 中间      更新时间:2023-10-16

请看继承:

interface IArray
{
   virtual unsigned __int8* GetAddress() const = 0;
   virtual unsigned int GetItemCount() const = 0;
   virtual unsigned int GetItemSize() const = 0;
};
template<class T>
class CustomArrayT : public IArray
{
public:
   virtual unsigned __int8* GetAddress() const;
   virtual unsigned int GetItemCount() const;
   virtual unsigned int GetItemSize() const;

   T& GetItem(unsigned int index);
};
interface IFloatArray : public CustomArrayT<float>
{
   virtual IFloatArray* GetCompressedData() const = 0;
};
class ShannonFloatArray : public IFloatArray
{
public:
   virtual IFloatArray* GetCompressedData() const;
};
class FourierFloatArray : public IFloatArray
{
public:
   virtual IFloatArray* GetCompressedData() const;
};
class MickyMouseFloatArray : public IFloatArray
{
public:
   virtual IFloatArray* GetCompressedData() const;
};

问题的主要目标是继承IFloatArray -> CustomArrayT:接口继承一些非抽象类。我不想支持多重继承。但是我需要所有下树类都具有类CustomArrayT的功能和实现接口IFloatArray。

这种树的优缺点是什么?

怎么能用另一种方式来完成呢?也许是某种模式?

我会这样做:

template<class T>
class IArray {
public:
  virtual int size() const=0;
  virtual T map(int index) const=0;
};

所有的指针都是不必要的。

你的继承制的一个问题是IArray接口到底是干什么用的?

如果您希望将任何实例化的派生类作为引用或指向 IArray 的指针传递,则无法从多态性中受益,因为在添加或检索任何包含的值之前需要向下转换。 (因为 IArray 无法在不声明要获取或设置的类型的情况下定义 getter 或 setter 函数,并且该类型取决于实例化的派生类。

为什么基类不能模板化? "基"类应该是 CustomArray,而"派生"类可以说是 typedef。

为什么不使用std::vector? (可能,为了练习。