类树中间的接口声明 (C++)
Declaration of interface in middle of classes tree (C++)
请看继承:
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? (可能,为了练习。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别