[英]Using copy constructors on iterators
I'm trying to make iterator. 我正在尝试制作迭代器。 As a constructor I have:
作为构造函数,我有:
iterator(Node* node)
{
it = node;
}
and copy constructor: 和复制构造函数:
iterator(const iterator& x)
{
it = x.it;
}
I was told that using first one is not a good idea while having second one(which is better) 我被告知使用第一个不是一个好主意,而第二个(这是更好的)
I'm not sure how to use second approach in methods like this: 我不确定如何在这样的方法中使用第二种方法:
typedef iterator<Key, Info> ringIterator;
ringIterator begin()
{
return ringIterator(any);
}
A copy constructor is a constructor, that accepts a (usually, but not necessarily, constant) reference to the same type. 复制构造函数是一个构造函数,它接受对同一类型的(通常但不一定是常量)引用。
Therefore, iterator(Node *)
is not a copy constructor. 因此,
iterator(Node *)
不是 复制构造函数。 It is a constructor from some internal entity of your collection. 它是来自您集合的某个内部实体的构造函数。
You do need that constructor in the implementation of the begin()
(and end()
and other methods returning iterators) of the collection. 在集合的
begin()
(和end()
以及其他返回迭代器的方法begin()
的实现中,您确实需要该构造函数。 However: 然而:
explicit
so it won't be used for implicit conversion. explicit
因此它不会用于隐式转换。 friend
declaration. friend
声明。 You should probably nest the iterator and the internal Node
type in the collection; Node
类型嵌套在集合中; that way the Node
itself can be private (but you don't show the complete structure of the collection you are writing, so I can't say for sure how to put this together). Node
本身可以是私有的(但是你没有显示你正在编写的集合的完整结构,所以我不能肯定地说如何把它放在一起)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.