Click to See Complete Forum and Search --> : operator*


kakalake
December 29th, 2002, 04:36 PM
Hello!
I have a general question. When i define an iterator what do the operator * have to return. For example: If i want to implement a string iterator like this...

template<class C>
class StringIt : public iterator<...>
{

};

What does the iterator-class have to return when i invoke the operator *. Must it be the element of the container ( in this example a reference of a char ) or something else? What do i have to use for the second template parameter for the inherited class iterator?

thanks

galathaea
December 29th, 2002, 08:05 PM
You can return anything you like. Its often helpful to return the actual dereference of the iterator's pointee, something like

template <class MyStringObjectType>
class Iterator
{
public:
// ... whatever else goes here
MyStringObjectType& operator*() const
{
// ... maybe you want to do something here
return *pointee_;
}
private:
MyStringObjectType* pointee_;
};

In this case use the class for your iterator's template parameter. I hope this helps you...