I am attempting to imitate the way STL iterators are declared by using inheritance and nested classes.
I have a parent class Iterable
with nested class iterator
.
class Iterable
{
public:
class iterator
{ };
};
I have two classes, List
and Vector
, which inherit from Iterable
in order to gain the nested class iterator
.
class List : public Iterable
{ };
class Vector : public Iterable
{ };
This allows declaration of iterators like this.
List::iterator lIte;
Vector::iterator vIte;
However I would like to have compile time errors when attempting to mix iterators from different derived classes. E.g.
lIte = vIte; // throw a compile error
But, since the iterator
s come from the same base class, Iterable
, the statement above will compile just fine.
An example work around would be to define a new nested class iterator
which will inherit from Iterable::iterator
in each class that inherits from Iterable
(List
and Vector
). E.g.
class List : public Iterable
{
public:
class iterator : Iterable::iterator // This "specializes" the datatype of iterator
{ };
};
class Vector : public Iterable
{
public:
class iterator : Iterable::iterator
{ };
};
Doing the above will successfully cause a compile time error when attempting:
List::iterator lIte;
Vector::iterator vIte;
lIte = vIte;
However this is a sub-optimal work around as:
- It involves code duplication in every class that inherits from
Iterable
.
- It requires C++11 in order for the duplicated
iterator
classes to inherit appropriate constructors from Iterable::iterator
.
My question is, how can I make List::iterator
and Vector::iterator
different types without duplicating code or using C++11 constructor inheritance?