C
CoolPint
If I were to design my own container and its iterators, I understand
that I need to provide both "iterator" and "const_iterator" so that
begin() and end() return appropriate ones depending on the
"constantness" of the container object. I also note that is it done
through overloading begin() and end() like below:
iterator begin();
const_iterator begin() const;
So it seems to me that I need to have two separate iterator classes
which are very similar except that const_iterator's operator*()
returns const reference. I also need to provide conversion from
iterator to const_iterator (probably by overloading copy constructor
of const_iterator class to accept iterator class objects) so that
iterator objects can be assigned to const_iterator objects.
Or I could use inheritance to derive iterator from const_iterator.
I hope my understanding so far is correct up to now.
Can I achieve the same effect by doing something like below rather
than having two different classes which are almost identical?
template <typename T>
class Container {
public:
class ContainerIterator;
typedef ContainerIterator iterator;
typedef const ContainerIterator const_iterator;
// other member functions
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
// private members
};
template <tyename T>
class Container<T>::ContainerIterator {
friend class Container<T>;
public:
ContainerIterator();
T & operator*();
const T & operator* () const; // so that const_iterator can be
deferenced
iterator & operator++();
const_iterator & operator++() const; // so that const_iterator can
be moved
iterator operator++(int);
const_iterator operator++(int) const;
iterator & operator--();
const_iterator & operator--() const;
iterator operator--(int);
const_iterator operator--(int) const;
bool operator==(const ListIterator &) const;
bool operator!=(const ListIterator &) const;
private:
// private members
};
I would appreciate any comment on whether this would be a viable
alternative or if not, what kind problems this might create. Thank you
in advance.
that I need to provide both "iterator" and "const_iterator" so that
begin() and end() return appropriate ones depending on the
"constantness" of the container object. I also note that is it done
through overloading begin() and end() like below:
iterator begin();
const_iterator begin() const;
So it seems to me that I need to have two separate iterator classes
which are very similar except that const_iterator's operator*()
returns const reference. I also need to provide conversion from
iterator to const_iterator (probably by overloading copy constructor
of const_iterator class to accept iterator class objects) so that
iterator objects can be assigned to const_iterator objects.
Or I could use inheritance to derive iterator from const_iterator.
I hope my understanding so far is correct up to now.
Can I achieve the same effect by doing something like below rather
than having two different classes which are almost identical?
template <typename T>
class Container {
public:
class ContainerIterator;
typedef ContainerIterator iterator;
typedef const ContainerIterator const_iterator;
// other member functions
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
// private members
};
template <tyename T>
class Container<T>::ContainerIterator {
friend class Container<T>;
public:
ContainerIterator();
T & operator*();
const T & operator* () const; // so that const_iterator can be
deferenced
iterator & operator++();
const_iterator & operator++() const; // so that const_iterator can
be moved
iterator operator++(int);
const_iterator operator++(int) const;
iterator & operator--();
const_iterator & operator--() const;
iterator operator--(int);
const_iterator operator--(int) const;
bool operator==(const ListIterator &) const;
bool operator!=(const ListIterator &) const;
private:
// private members
};
I would appreciate any comment on whether this would be a viable
alternative or if not, what kind problems this might create. Thank you
in advance.