如何创建模板化对象的数组/向量
How to create an array/vector of templated objects
我有一个模板类,它是一个模板,作为MFC CWnd
对象的包装器。
在主类中,我想有一个面板控件的集合,但我不明白如何创建它们的数组,因为它们显然派生自不同的基类(即 CButtonCtrl、CListBox 等)。
template<typename W>
class Component : public W
{
public:
Component(uint_t nId = -1, CWnd *pParent = NULL, const char *pName = NULL)
: mParent(pParent)
, mName(pName)
, mId(nId)
{
//std::cout << ((mName != NULL) ? mName : "NULL") << ": " << (void *)this << ": Id:" << mId << " Parent:" << (void *)mParent << std::endl;
}
const char *getName(void) const { return mName; }
void setId(uint_t nId) { mId = nId; }
uint_t getId(void) const { return mId; }
void setParent(CWnd *pParent) { mParent = pParent; }
CWnd *getParent() const { return mParent; }
private:
uint_t mId;
CWnd *mParent;
const char *mName;
};
主类:
class Panel : public Component<CDialog>
{
public:
Panel(uint_t nId = -1, CWnd *pParent = NULL, const char *pComponentName = NULL);
virtual ~Panel(void) {};
virtual void CreateWnd(CWnd *pParent = NULL);
template <typename W>
void addComponent(Component<W> *pComponent)
{
}
template <typename W>
void removeComponent(Component<W> *pComponent)
{
}
void Show(bool bShow = true) override;
protected:
typedef std::vector<Component<?> *> Components;
private:
Components mComponents;
};
在 Java 中,像这样的未知泛型有一个语法Component<?>
所以我可以在数组中存储公共对象的集合,但我不知道如何在C++中做到这一点。我知道我必须使用指针,因为slcing,但这不是这里的问题。
使用 Visual Studio 2010。
您可能想尝试boost::any
或std::any
相关文章:
- 将数组/向量发送到 c++ 脚本的 cython 示例
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 将指向多维数组向量中的单个项的指针传递给函数 c++
- 数组向量的文件 I/O
- 在一行中初始化和引入数组向量的正确方法?
- C++ 返回具有最小硬币的数组/向量以获得价值动态规划
- 将整数向量转换为字节数组向量,然后调用每个字节数组
- 我可以在不循环访问数组/向量的情况下检查数组/向量中的单词吗?
- (C )创建可以从功能访问的动态全局数组/向量
- 提升是否具有用于数组向量的智能指针
- 将函数应用于数组向量的元素
- 数组向量的内存布局是什么
- 获取数组/向量末尾地址的未定义行为
- "Empty"数组\向量成员 C++
- 将字符串向量转换为双2-D数组向量
- 排序多维数组/向量
- 如何创建模板化对象的数组/向量
- 如何在数组向量上使用 std::find
- 无法删除C++中的数组/向量
- 具有元素自定义构造函数的对象数组/向量