Immutable container with mutable content

后端 未结 4 1799
遇见更好的自我
遇见更好的自我 2021-01-04 15:49

The story begins with something I thought pretty simple :

I need to design a class that will use some STL containers. I need to give users of the class access to an

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 16:02

    You need a custom data structure iterator, a wrapper around your private list.

    template
    class inmutable_list_it {
    public:
        inmutable_list_it(std::list* real_list) : real_list_(real_list) {}
    
        T first() { return *(real_list_->begin()); }
    
        // Reset Iteration
        void reset() { it_ = real_list_->begin(); }
    
        // Returns current item 
        T current() { return *it_; } 
    
        // Returns true if the iterator has a next element.
        bool hasNext(); 
    
    private:
        std::list* real_list_;
        std::list::iterator it_;
    };
    

提交回复
热议问题