F
flopbucket
Hi,
Say I have a baseclass B that has many derived classes, let's say
C..Z, for example:
class B
{
};
class C : public B {};
class D : public B {};
class E : public B {};
....
Also assume that these classes are provided as is, that is, I can not
make any changes to them (the real classes contain many data members,
etc).
Now, there is a factory method that creates the correct class based on
input from some external source. Something like:
B *b = SomeFactoryMethod();
Of course SomeFactoryMethod will actually return any of the dervied
classes depending on the external information.
Now here is my question. There are a set of methods such as:
processObject(C *);
processObject(D *);
processObject(E *);
.... for all derived types.
Given the base clas pointer returned from the factory, how can I call
the correct method? Obviously I can have a bunch of dynamic_cast<>'s
but that does not seem the best way. I was thinking I could do
something with templates and typeinfo?
Any ideas? Please remember that I can not change the existing classes.
Say I have a baseclass B that has many derived classes, let's say
C..Z, for example:
class B
{
};
class C : public B {};
class D : public B {};
class E : public B {};
....
Also assume that these classes are provided as is, that is, I can not
make any changes to them (the real classes contain many data members,
etc).
Now, there is a factory method that creates the correct class based on
input from some external source. Something like:
B *b = SomeFactoryMethod();
Of course SomeFactoryMethod will actually return any of the dervied
classes depending on the external information.
Now here is my question. There are a set of methods such as:
processObject(C *);
processObject(D *);
processObject(E *);
.... for all derived types.
Given the base clas pointer returned from the factory, how can I call
the correct method? Obviously I can have a bunch of dynamic_cast<>'s
but that does not seem the best way. I was thinking I could do
something with templates and typeinfo?
Any ideas? Please remember that I can not change the existing classes.