pointer to std::vector of arbitrary type (or any other templated class)

后端 未结 3 1582
时光取名叫无心
时光取名叫无心 2021-01-22 00:13

let\'s say i want to have a member variable for a pointer to std::vector but i do not want to specify what type of variable it stores. I want to access only those functions that

3条回答
  •  长情又很酷
    2021-01-22 00:46

    You could do this:

    class vector_container_base
    {
    public:
        ~vector_container_base() {}
    
        virtual std::size_t size() const = 0;
    };
    
    template 
    class vector_container :
        public vector_container_base
    {
    public:
        typedef std::vector vector_type;
    
        std::size_t size() const
        {
            return mVector.size();
        }
    
    private:
        vector_type mVector;
    };
    

    And so on, but I doubt this is too useful in any real situation.

提交回复
热议问题