I faced exactly this problem before. Although there are ways to solve your problem, you should probably give up the idea of a vector base class. Instead, you should probably imitate the way you create the c ++ STL container.
STL , . std::vector Container, Container. - , . . , , Container.
Container, , , typedef value_type, typedef iterator const_iterator. , begin() end(), , ..
, Vector, , , . , . , STL, . , STL, , , STL (, std::sort) .
:
class VectorImplA
{
public:
typedef VectorImplAIterator iterator;
iterator begin();
iterator end();
};
class VectorImplB
{
public:
typedef VectorImplBIterator iterator;
iterator begin();
iterator end();
};
template <typename VectorConcept>
void doSomeOperations(VectorConcept &container)
{
VectorConcept::iterator it;
it = container.begin();
}
int main()
{
VectorImplA vecA;
VectorImplB vecB;
doSomeOperations(vecA);
doSomeOperations(vecB);
}
, , ( !):
struct IteratorBase
{
virtual void next() = 0;
};
struct IteratorA : IteratorBase
{
void next() {};
};
struct IteratorB : IteratorBase
{
void next() {};
};
class Iterator
{
IteratorBase *d_base;
public:
void next() { d_base->next(); }
};