Search code examples
c++stlconst-iterator

Iterator over pointer to pointer structure


I have written a custom container over two structures: an array of T T* _a and an array of pointers to T T** _b. The first array is a simple one, the second array contains pointers to the cells of the first one.

The iterators for both structures are const and random access ones. I have implemented all the methods, and the iterator for the first structure works fine. For the second one... not so much. Compiling, I get this error when I try to iterate over the elements with begin() and end():

error: invalid conversion from ‘int**’ to ‘const int**’ [-fpermissive]
         return const_iterator(_b);

But I cannot figure out how to solve it. I understand that it's because the constructor asks for a pointer to a pointer to a constant

const_iterator(const T **p) : ptr(p) {}

But that's not what I have, as _b is an attribute of the container and it's not constant. For the other iterator it works just fine (the only thing changing are a couple of more dereferences in the iterator class).

Here's the code of the problematic iterator. Here's the one that works. What should there be other differences other than the one I made? Thanks!


Solution

  • The answer is as Igor said in the comments about int** conversion to const int**. I thought it worked like int* but it doesn't. I found more info here.