简体   繁体   English

迭代器指向指针结构的指针

[英]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 . 我已经在两个结构上编写了一个自定义容器:T T* _a数组和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. 这两种结构的迭代器都是const和随机访问的。 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() : 编译时,尝试使用begin()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. 但这不是我所拥有的,因为_b是容器的属性,并且不是恒定的。 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! 谢谢!

The answer is as Igor said in the comments about int** conversion to const int**. 答案就是Igor在有关将int **转换为const int **的评论中所说的。 I thought it worked like int* but it doesn't. 我认为它像int *一样工作,但事实并非如此。 I found more info here . 我在这里找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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