A pointer to abstract template base class?

前端 未结 2 1693
梦毁少年i
梦毁少年i 2021-02-04 13:35

I cannot figure this out. I need to have an abstract template base class, which is the following:


template  class Dendrite
{
    public:
        Den         


        
相关标签:
2条回答
  • 2021-02-04 14:16

    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*);
    };
    
    0 讨论(0)
  • 2021-02-04 14:20

    Yes it is possible. Just make sure to provide virtual functions, and virtual destructor. In addition, you can use typeid to get the actual type (as well as dynamic_cast to check the type)

    0 讨论(0)
提交回复
热议问题