I am studing the polymorphism solution suggested here, but I can't think of how polymorphism can be used for dynamic typing.
I cannot use inheritance, since the specific datatypes reside in the child-objects TitleSky and TitleMHW1 , and obviously only children can inherit from their parents and not the other way around.
So I think I must use the override mechanism, to override some dummy-datatype in the BaseTitle class, but I understand that you can only override methods and not variables (am I correct in that?).
So then I am forced to use an overriding method, but with a method I MUST specify compile-time data-types, so not usable for overriding a data-type run-time.
This is what I have right now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class BaseTitle
{
public:
virtual int SetFormat (const u_char * Data, int Length)//TODO do I need this dummy?
{
sTitle *Title = (sTitle *) Data;
return BaseTitle::ReadTitle(Data, Length,Title);
}
};
class TitleMHW1:public BaseTitle
{
public:
virtual int SetFormat (const u_char * Data, int Length)
{
sTitleMHW1 *Title = (sTitleMHW1 *) Data;
return BaseTitle::ReadTitle(Data, Length,Title);//damn, now I must specify title type again,
//and all these classes where meant to prevent this
}
};
|
Another possibility would be to write a separate method for every child, but then I get massive code replication because only the data-structures differ slightly, the rest is all the same...
Am I making a mistake here, or is the solution proposed not right?
Thanks in advance!