Using copy constructors on iterators

I am trying to make an iterator. As a constructor, I:

iterator(Node* node)
{
    it = node;
}

and copy constructor:

iterator(const iterator& x)
{
    it = x.it;
}

I was told that using the first one is not a good idea, and the second (which is better)

I am not sure how to use the second approach in such methods:

typedef iterator<Key, Info> ringIterator;

ringIterator begin()
{
    return ringIterator(any);
}
+4
source share
1 answer

A copy constructor is a constructor that takes a reference (usually, but not necessarily constant) to the same type.

Therefore, iterator(Node *)it is not a copy constructor. This is a constructor from some internal object of your collection.

You need this constructor in the implementation begin()( end()and other methods that return iterators) of the collection. However:

  • explicit, .
  • . , friend. , Node; Node ( , , , ).
+5

Source: https://habr.com/ru/post/1690293/


All Articles