简体   繁体   中英

C++ iterators: Can iterators to an abstract class be implemented as a nested class?

I was wondering if one could implement custom iterators to an abstract class as an embedded class with appropriate overloading of operators and methods. That is, if it would be alright as a starting point to start as:

template <typename T>
class my_class {
public:
    .
    .
    class iterator {
    public:
        .
        .
    };
};

I have tried understanding several different ways of implementing custom iterators, but it would be easier to grasp if layout can be a bit simplified. The methods like begin(), end(), etc., can be now implemented in my_class along with any other types required.

Would such an implementation be compatible with STL algorithms? I remember having implemented such an iterator class that seemed to work, but I am not sure if it would be ok in general.

There is no requirement where the type should be defined. It has to be accessible as container:: iterator (and container:: const_iterator ) and there is no reason why it couldn't be defined right there. A nested type is a type just like any other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM