Reputation: 1616
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);
}
Upvotes: 4
Views: 743
Reputation: 76276
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. 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. However:
explicit
so it won't be used for implicit conversion.friend
declaration. You should probably nest the iterator and the internal Node
type in the collection; 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).Upvotes: 5