I have the task to write own containers Linked_list and Array_list. I have one interface for them:
typedef int value_type;
class Container
{
public:
class Iterator
{
public:
Iterator();
Iterator(value_type* other);
Iterator(const Iterator& other);
Iterator& operator=(const Iterator& other);
...
};
Container();
Container(const Container& other);
~Container();
virtual value_type& front() const=0;
virtual value_type& back() const=0;
virtual Iterator begin() const=0; //
...
};
I did derived classes Linked_list and Array_list:
class Linked_list:public Container
{
public:
long int cur_size;
List elem;
static Link end_;
class Iterator: public Container::Iterator
{
friend Linked_list;
Link *p;
};
Iterator begin() const; //overriding virtual function return type differs ...
...
}
I thinks it’s all wrong. should nested class Linked_list::Iterator be a derived class?
Is it possible to do this, if I can’t change the interface?
Taking into account your design constraints that you cannot use templates, than one thing should change: add interface
IteratorImpl. Thus you can makeclass Iteratorfrom baseclass Containernon virtual. It needs to be non-virtual since STL-alike iterators should have value semantics. See pimpl idiom for more details how it works!Like this:
Then in your derived just implement IteratorImpl:
These firstNode and nodeAfterLastNode are just my guess – use whatever you need to implement the IteratorImpl interface…