Iterator in Design Pattern

D

Da Wang

Hi,all,

I am reading the Iterator part of Design Pattern. It defines a basic
Iterator interface as shown below. My question is: what is the use of
the protected constructor? Maybe that does not matter but just in case I
neglect something important.
many thanks.
-------------------------------
template <class Item>
class Iterator {
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() const = 0;
virtual Item CurrentItem() const = 0;
protected:
Iterator();
};
--
Life is an opportunity to do something.
.-._
o_oo'_)
`._ `._
`, \
//_(_)_/
~~
 
A

Alf P. Steinbach

* Da Wang:
I am reading the Iterator part of Design Pattern. It defines a basic
Iterator interface as shown below. My question is: what is the use of
the protected constructor?
Maybe that does not matter but just in case I neglect something important.
many thanks.
-------------------------------
template <class Item>
class Iterator {
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() const = 0;
virtual Item CurrentItem() const = 0;
protected:
Iterator();
};


It's a redundant way of restricting instantiation to derived classes.

It's redundant because this class has pure virtual members.

And it's redundantly redundant because there's nothing to initialize,
so the compiler-generated default constructor would do.

By the way, as a general iterator class this one won't be very useful
for expensive to copy items, and it should have virtual destructor.

In short, don't take this code as an example of perfect code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top