Interfaces and covariance problem

前端 未结 5 2043
我在风中等你
我在风中等你 2021-01-05 07:29

I have a particular class that stores a piece of data, which implements an interface:

template
class MyContainer : public Container

        
5条回答
  •  广开言路
    2021-01-05 08:18

    If the usage is supposed to be similar to stdlib, then the iterator needs to be a value object, because it normally gets copied by value a lot. (Also, otherwise what would the begin and end method return a reference to?)

    template 
    class Iterator
    {
        shared_ptr it;
    public:
        Iterator(shared_ptr);
        T& operator*() { it->deref(); }
        T* operator->() { return &it->deref(); }
        Iterator& operator++() { it->inc(); return *this; }
        etc.
    };
    

提交回复
热议问题