如何在自定义列表迭代器类中将迭代器转换为const_iterator? [英] How do I convert iterator to const_iterator in my custom list iterator class?

查看:147
本文介绍了如何在自定义列表迭代器类中将迭代器转换为const_iterator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用C ++实现自己的双向链接列表(my_list)代码,尤其是列表的迭代器类.我的问题是我想从迭代器到const_iterator进行隐式转换,以便例如代码my_list::iterator it = l.begin();其中lmy_list的实例进行编译.但是,在没有编译器抱怨的情况下,我找不到解决方法.

I am trying to implement my own doubly-linked list (my_list) code in C++, and in particular an iterator class for my list. My problem is I want to have an implicit conversion from iterator to const_iterator so that for example the code my_list::iterator it = l.begin(); where l is an instance of my_list compiles. However, I cannot find a way to do this without my compiler complaining.

这是实现列表节点和迭代器类的代码:

Here is the code implementing the list nodes and the iterator class:

template<class T> class node {
    node(const T& t = T()):data(t),next(0),prev(0) {}
    T data;
    node* next;
    node* prev;

    friend class my_list<T>;
    friend class my_list_iterator<T>;
};

template<class T> class my_list_iterator {
    public:
            // increment and decrement operators
            my_list_iterator operator++();
            my_list_iterator operator++(int);
            my_list_iterator operator--();
            my_list_iterator operator--(int);

            // bool comparison iterators
            bool operator==(const my_list_iterator& other) const {return pos_==other.pos_;}
            bool operator!=(const my_list_iterator& other) const {return pos_!=other.pos_;}

            // member access
            T& operator*() const {return pos_->data;}
            T* operator->() const {return &(pos_->data);}

            // implicit conversion to const iterator
            operator my_list_iterator<const T>() {return my_list_iterator<const T>(pos_);}
    private:
            node<T>* pos_;
            explicit my_list_iterator(node<T>* p=0):pos_(p) {}
            friend class my_list<T>;
};

我已经省略了my_list实现,但是如果您认为它是相关的,则可以包括它.当我测试此代码时,它将无法在GCC上编译,并显示以下错误:

I have omitted the my_list implementation but if you think it is relevant I can include it. When I test this code, it won't compile on GCC with the following error:

In file included from test.cpp:2:
my_list.h: In instantiation of ‘my_list_iterator<T>::operator my_list_iterator<const T>() [with T = int]’:
test.cpp:12:49:   required from here
my_list.h:37:48: error: no matching function for call to ‘my_list_iterator<const int>::my_list_iterator(node<int>*&)’
   operator my_list_iterator<const T>() {return my_list_iterator<const T>(pos_);}
                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_list.h:40:12: note: candidate: ‘my_list_iterator<T>::my_list_iterator(node<T>*) [with T = const int]’
   explicit my_list_iterator(node<T>* p=0):pos_(p) {}
            ^~~~~~~~~~~~~~~~
my_list.h:40:12: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘node<const int>*’
my_list.h:20:25: note: candidate: ‘constexpr my_list_iterator<const int>::my_list_iterator(const my_list_iterator<const int>&)’
 template<class T> class my_list_iterator {
                         ^~~~~~~~~~~~~~~~
my_list.h:20:25: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘const my_list_iterator<const int>&’
my_list.h:20:25: note: candidate: ‘constexpr my_list_iterator<const int>::my_list_iterator(my_list_iterator<const int>&&)’
my_list.h:20:25: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘my_list_iterator<const int>&&’

有人可以帮我解决我做错的事情吗?

Can someone help me with what I'm doing wrong?

推荐答案

my_list.h:40:12: note: candidate: ‘my_list_iterator<T>::my_list_iterator(node<T>*) [with T = const int]’
   explicit my_list_iterator(node<T>* p=0):pos_(p) {}
            ^~~~~~~~~~~~~~~~
my_list.h:40:12: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘node<const int>*’

A node<int>node<const int>是不相关的类型.您不能将指向node<int>的指针传递给需要指向node<const int>的指针的函数.

A node<int> and a node<const int> are unrelated types. You cannot pass a pointer to a node<int> to a function that expects a pointer to a node<const int>.

您可以将const向上移动一层,并在节点类型上使用模板作为模板,而不是在包含的类型上模板您的迭代器类:

Instead of templating your iterator class on the contained type you could move the const up one level and template your iterator on the node type:

template<class Node> class my_list_iterator {
public:

    //...

    // member access
    auto& operator*() const {return pos_->data;}
    auto* operator->() const {return &(pos_->data);}

    // implicit conversion to const iterator
    operator my_list_iterator<const Node>() {return my_list_iterator<const Node>{pos_};}
private:
    Node* pos_;
    explicit my_list_iterator(Node* p=0):pos_(p) {}
    friend class my_list<type>;
};

template <class T> class my_list {
public:
    using iterator = my_list_iterator<node<T>>;
    using const_iterator = my_list_iterator<const node<T>>;
    //...
};

实时演示

现在,您要将指向node<int>的指针传递给需要指向const node<int>的指针的函数,这很好.

Now you're passing a pointer to a node<int> to a function expecting a pointer to a const node<int>, and that's fine.

这篇关于如何在自定义列表迭代器类中将迭代器转换为const_iterator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆