Objects are normally
not created with new in C++ - and when you do, you have to delete them afterwards.
You should create the iterator as follows:
StringCharacterIterator s("abcd");
Or a more common syntax for constructors with one parameter:
StringCharacterIterator s="abcd";
And instead of having a function called nextCharacter, it would be better to use operator++ for this.
Instead of haveReachedEndOfString, you can make use of operator bool (conversion to bool), which shortens your loop condition to "s". To get the current character, making use of operator* (dereferencing) would be appropriate.
In summary, it should be possible to rewrite the code to look like this:
1 2 3 4 5
|
StringCharacterIterator s="abcd";
for(;s;++s)
{
std::cout << *s << std::endl;
}
|
I'm not sure if you're aware of it, but it's already possible to iterate over a string:
1 2
|
string str="abcd";
for (string::iterator it=str.begin();it!=str.end();++it)cout << *it << endl;
|
Or with C++11:
for (auto c : str)cout << c << endl;
or
for_each(begin(str),end(str),[](char c) {cout << c << endl;});