Here, member variable "str_" is used to point to the string object to which the iterator is initialised.
[A] Aim : Eliminate raw pointer "str_" used in the above implementation.
Few observations :
[1] Since string is not allocated by iterator (not owner) rather it is just an object pointing to string object, "unique_ptr<string*> str_" doesn't look like a feasible solution.
[2] I am not very sure if using shared_ptr would be a good idea either as it is pretty expensive.
If we use reference, then how do we implement "operator =" ?
You wouldn't. An iterator of this nature would apply to a single container (such as a string) and that would be all there would be to it. The iterator would be created for use with a single, existing container only.
kapil's iterator type has one big advantage over std::string::iterator in that it can handle the string data being reallocated, and it would also be relatively easy to implement bounds-checking if that is desirable.
If copying the iterator just copies each member as shown above there isn't really a need for you to define your own copy operations because the defaulted copy constructor and copy assignment operators would do the exact same thing. There is also no need to define move constructor or move assignment operator because there is no clever optimization that you can do in this case to make it move faster than a copy.
If you want an iterator that is the same as the one provided by std::string.... use the one provided by std::string.
You're writing your own because you want it to do something else, otherwise why would you be writing it. So what's different about yours?
Apologies for putting the requirement in an improper way.
Suppose we have requirement like this :
[i] Implement string iterator without using any library provided iterators.
[ii] Usage of raw pointers is not allowed.
[iii] You should be able to do "it1 = it2" where it1 and it2 are iterators to different strings.
Suppose we have requirement like this :
[i] Implement string iterator without using any library provided iterators.
[ii] Usage of raw pointers is not allowed.
[iii] You should be able to do "it1 = it2" where it1 and it2 are iterators to different strings.
Also a raw pointer is also commonly known as a void* so you should ask your teacher if that is what you should avoid (but then again, people have their own interpretations).