C++ vector with dynamic item size

前端 未结 9 2352
无人及你
无人及你 2021-01-14 16:54

the C++ STL vector has a lot of decent properties, but only works when the size of each item is known at run-time.

I would like to have a vector class that features

9条回答
  •  孤独总比滥情好
    2021-01-14 17:20

    Use std::vector where item is a wrapped smart pointer. The "item" class make a pointer look like a plain value :

    class item {
    private:
        boost:unique_ptr p;
    public:
        // ...
        public item(item that);
    
        public int someFunction() {
           // Forwarded
           return p->somefunction();
        }
    };
    
    class base {
        virtual ~base();
        // This is needed in order to implement copy of the item class
        virtual base* clone();
    };
    
    public item::item(item that) : p(that.p.clone()) {}
    
    class some_real_type() : public base {
       // ....
    }
    

提交回复
热议问题