How to make my custom type to work with “range-based for loops”?

后端 未结 9 2296
长情又很酷
长情又很酷 2020-11-22 08:06

Like many people these days I have been trying the different features that C++11 brings. One of my favorites is the \"range-based for loops\".

I understand that:

9条回答
  •  眼角桃花
    2020-11-22 08:48

    Chris Redford's answer also works for Qt containers (of course). Here is an adaption (notice I return a constBegin(), respectively constEnd() from the const_iterator methods):

    class MyCustomClass{
        QList data_;
    public:    
        // ctors,dtor, methods here...
    
        QList::iterator begin() { return data_.begin(); }
        QList::iterator end() { return data_.end(); }
        QList::const_iterator begin() const{ return data_.constBegin(); }
        QList::const_iterator end() const{ return data_.constEnd(); }
    };
    

提交回复
热议问题