Suppose there is some template class that uses a container from the STL library:

#include <array> template <typename T, size_t D> class default_name { std::array<Type, Dim> container; ... } 

I want to override begin() , end() and other functions associated with the iterators for my class so that they use the iterators of the inner container and, accordingly, its begin() , end() and others functions.

In this case, I need to specify the return value of the member functions of my class.

Actually, the question is how to get or refer to the type of iterators of the inner container?

    1 answer 1

     #include <array> template <typename T, size_t D> class default_name{ typedef std::array<T, D> Container; Container container; public: typedef typename Container::iterator Iterator; typedef typename Container::const_iterator ConstIterator; Iterator begin(){ return container.begin(); } Iterator end(){ return container.begin(); } ConstIterator begin() const{ return container.begin(); } ConstIterator end() const{ return container.begin(); } }; 
    • The question is related, but a little further from the topic. When will the const iterator function be called? - Gordory
    • one
      @Gordory, to be honest, I didn’t understand what the "const iterator function" means. If you meant the constant methods begin and end , then they are called on constant objects, references, and pointers. const default_name *ptr; ptr->begin(); //ConstIterator begin() const const default_name *ptr; ptr->begin(); //ConstIterator begin() const - yrHeTaTeJlb
    • Crooked formulated. Yes, exactly, thanks. - Gordory