且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

指向抽象模板基类的指针?

更新时间:2023-11-13 13:01:10

通常这是通过从接口类 IE 继承的模板来完成的:

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

然后你的 IDendrite 类可以存储为指针:

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

给你

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*);
};