What I want to do is this:
class Ba { }
class Da : public Ba {}
class Db : public Ba {}
class Bb // abstract base class that must not be a template.
{
void Process()
{
list<Ba*>::iterator pos;
// I known the derived class will also derive
// from list<Dx*> where Dx derives from Ba
for(pos = this->begin(); pos < this->end(); pos++)
something(*pos);
}
}
template<T> class L : public Bb , list<T*> // T is always derived from Ba
{
}
But that's invalid. So what's the next best thing?
An alternately formulation has a global Process
get a passed a Bb
pointer where the type is not know til run-time:
void GlobalProcess(Bb* bb) // don't know what kind of Bb (which L) got passed in.
{
list<Ba*>::iterator pos;
// I known the derived class will also derive
// from list<Dx*> where Dx derives from Ba
for(pos = bb->begin(); pos < bb->end(); pos++)
something(*pos);
}
I have a few reasons for doing this but about half of it is feeling out the edges of the C++ system.
The simplest form of the problem I can come up with is that I need to iterate over a list<D*>
from code where all the info I'm allowed to use at compile time is that D
derives from B
. This situation can arise from being passed a list<D>
via a pointer to an abstract base class or where the code its self is in a base class.
This would all work just fine if only list<D*>::iterator
were derived from list<B*>::iterator