指向抽象模板基类的指针? [英] A pointer to abstract template base class?
问题描述
我不知道。我需要一个抽象的模板基类,其中
如下:
template< class T> class Dendrite
{
public:
Dendrite()
{
}
virtual〜Dendrite()
{
}
virtual void Get(std :: vector< T&
protected:
std :: vector< T> _data;
};
现在,我从这里导出指定Dendrite的精确用法。
现在的问题。
如何创建一个指向没有特定类型的基类的指针的向量,
我想通过推送元素指定以后呢?像:
class Foo
{
public:
.. 。
private:
std :: vector< Dendrite *> _inputs; //!不幸的是,这不工作...
//!现在我可以稍后推送元素到这个向量如
//!
//! _inputs.push_back(new DeriveFromDendrite< double>())和
//! _inputs.push_back(new DeriveFromDendrite< int>())。
};
这是可能还是我缺少一些非常基本的东西?
<通常,这是通过从接口类IE继承的模板完成的:
code> template< class T> class Dendrite:public IDendrite
{
public:
Dendrite()
{
}
virtual〜Dendrite()
{
}
void Get(std :: vector< T>& o)= 0;
protected:
std :: vector< T> _数据;
};
,然后您可以将IDendrite类存储为指针:
std :: vector< IDendrite *> m_dendriteVec;
但是,在你的情况下,你将模板参数作为你的接口的一部分。您也可能需要包装此类。
class IVectorParam
{
}
template< class T>
class CVectorParam:public IVectorParam
{
std :: vector< T& m_vect;
}
给您
class IDendrite
{
...
public:
virtual〜IDendrite()
virtual void Get(IVectorParam *) = 0;
}
template< class T> class Dendrite:public IDendrite
{
...
//我的get必须向下转换为o CVectorParam< T>
virtual void Get(IVectorParam *);
};
I cannot figure this out. I need to have an abstract template base class, which is the following:
template <class T> class Dendrite
{
public:
Dendrite()
{
}
virtual ~Dendrite()
{
}
virtual void Get(std::vector<T> &o) = 0;
protected:
std::vector<T> _data;
};
Now, I derive from this which specifies exact usage of Dendrite.
Now the problem.
How do I create a vector of pointers to the base-class with no specific type, which
I want to specify by pushing elements to it later? Something like:
class Foo
{
public:
...
private:
std::vector<Dendrite *> _inputs; //!< Unfortunately, this doesn't work...
//! Now I could later on push elements to this vector like
//!
//! _inputs.push_back(new DeriveFromDendrite<double>()) and
//! _inputs.push_back(new DeriveFromDendrite<int>()).
};
Is this possible or am I missing something very basic here?
Typically this is done by your template inheriting from an interface class, IE:
template <class T> class Dendrite : public IDendrite
{
public:
Dendrite()
{
}
virtual ~Dendrite()
{
}
void Get(std::vector<T> &o) = 0;
protected:
std::vector<T> _data;
};
and then you're IDendrite class could be stored as pointers:
std::vector<IDendrite*> m_dendriteVec;
However, in your situation, you are taking the template parameter as part of your interface. You may also need to wrap this also.
class IVectorParam
{
}
template <class T>
class CVectorParam : public IVectorParam
{
std::vector<T> m_vect;
}
giving you
class IDendrite
{
...
public:
virtual ~IDendrite()
virtual void Get(IVectorParam*) = 0;
}
template <class T> class Dendrite : public IDendrite
{
...
// my get has to downcast to o CVectorParam<T>
virtual void Get(IVectorParam*);
};
这篇关于指向抽象模板基类的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!