[英]How do I convert iterator to const_iterator in my custom list iterator class?
I am trying to implement my own doubly-linked list (my_list) code in C++, and in particular an iterator class for my list. 我正在尝试在C ++中实现自己的双向链接列表(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();
我的问题是我想从迭代器到const_iterator进行隐式转换,例如代码my_list::iterator it = l.begin();
where l
is an instance of my_list
compiles. 其中l
是my_list
编译的实例。 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>;
};
I have omitted the my_list implementation but if you think it is relevant I can include it. 我已经省略了my_list实现,但是如果您认为它是相关的,则可以包括它。 When I test this code, it won't compile on GCC with the following error: 当我测试此代码时,它不会在GCC上编译并出现以下错误:
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>
and a node<const int>
are unrelated types. node<int>
和node<const int>
是不相关的类型。 You cannot pass a pointer to a node<int>
to a function that expects a pointer to a node<const int>
. 您不能将指向node<int>
的指针传递给需要指向node<const int>
的指针的node<const int>
。
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: 除了可以在包含的类型上模板迭代器类之外,还可以将const
上移一层并在节点类型上模板化迭代器:
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>>;
//...
};
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. 现在,您将一个指向node<int>
的指针传递给一个期望指向const node<int>
的指针的const node<int>
,这很好。
This is one way to do it: 这是一种方法:
template <typename T>
class my_list {
public:
using iterator = my_list_iterator<T>;
using const_iterator = my_list_iterator<const T>;
const_iterator cbegin() const { return {/*...*/}; }
const_iterator cend() const { return {/*...*/}; }
const_iterator begin() const { return {/*...*/}; }
const_iterator end() const { return {/*...*/}; }
iterator begin() { return {/*...*/}; }
iterator end() { return {/*...*/}; }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.