S
suman.nandan
Hi C++ Experts !
I have a little weird requirement.
I have a base class, say B and lots of classes D1 .. Dn publicly
derived from it. Over the course of development the number of derived
classes may increase.
I want that before the execution of one of my particular code, I
should have a list of pointers to all the derived classes, say
std::list<B*> ptrList; // contains 'n' elements which are pointers to
D1 .. Dn.
I don't want to have anything like:
ptrList.push_back (new D1);
ptrList.push_back (new D2);
...
Also I don't want to instantiate any static variable of D1 .. Dn, so
that inside the constructor I may register the 'this' pointer.
All I want is the developer should just write the definition of
derived classes, possibly with some constraints and should not bother
about anything else.
Recently I read about 'TypeLists' in Modern C++ design. I was
wondering if I can use it here.
Inside the constructor of the derived classes, I may populate its type
in a 'TypeList'. The 'TypeList' will be full with 'n' elements at the
compile time itself.
Now if I only can do
for each type T in 'TypeList' {
// make sure that type T is derived class of B
// may be through
// assert (dynamic_cast<B*>(T));
ptrList.push_back (new T);
}
my problem will be solved.
Do you guys see any problem with this approach?
Any discussion on this will be appreciated.
Thanks in advance !!
Suman Nandan.
I have a little weird requirement.
I have a base class, say B and lots of classes D1 .. Dn publicly
derived from it. Over the course of development the number of derived
classes may increase.
I want that before the execution of one of my particular code, I
should have a list of pointers to all the derived classes, say
std::list<B*> ptrList; // contains 'n' elements which are pointers to
D1 .. Dn.
I don't want to have anything like:
ptrList.push_back (new D1);
ptrList.push_back (new D2);
...
Also I don't want to instantiate any static variable of D1 .. Dn, so
that inside the constructor I may register the 'this' pointer.
All I want is the developer should just write the definition of
derived classes, possibly with some constraints and should not bother
about anything else.
Recently I read about 'TypeLists' in Modern C++ design. I was
wondering if I can use it here.
Inside the constructor of the derived classes, I may populate its type
in a 'TypeList'. The 'TypeList' will be full with 'n' elements at the
compile time itself.
Now if I only can do
for each type T in 'TypeList' {
// make sure that type T is derived class of B
// may be through
// assert (dynamic_cast<B*>(T));
ptrList.push_back (new T);
}
my problem will be solved.
Do you guys see any problem with this approach?
Any discussion on this will be appreciated.
Thanks in advance !!
Suman Nandan.