can i declare an iterator outside of the container class?

Hi

I have created a container class for which I need to create a bespoke iterator (not merely exposing the iterator of the container's internal data type).

I think the normal way of doing this is along the following lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  class Container
  { 

     public:

     struct ContainerIterator
     {
       // specify container tags, pointer, methods etc
     };

     private:
     // internal data etc

  };


This is fine but I find the container class becomes somewhat cluttered as the declaration of the iterator can be somewhat lengthy.

Is there any reason why the iterator could not be declared in the same header file as the Container class but not as a nested declaration in the Container class?

Thanks
> I find the container class becomes somewhat cluttered
> as the declaration of the iterator can be somewhat lengthy.

We could do this:

1
2
3
4
5
6
7
8
9
10
template< typename T > struct container
{
    struct iterator ;
    // ...
};

template< typename T > struct container<T>::iterator
{
    // ...
};

http://coliru.stacked-crooked.com/a/db2c77777139ea53
Topic archived. No new replies allowed.