Reputation: 48139
I'm not sure how to build this out, but here is the basics. I have a class that reads a file. As it goes through, it has some things that are generically handled and for each "piece" (many pieces in a given file). As each "piece" is processed, it's SPECIFIC type is identified and I want an instance of another class by the specific type to be added (via new *instance() of that class. Here is the basics of what I'm trying to do.
This is the baseline I want to ultimately get to
class SpecificBaseline
{
public:
SpecificBaseline();
virtual void ThisHandledPerDerived();
}
Specific subclassed instances
class SubClass1 : SpecificBaseline
{ -- blah -- }
class AnotherSubClass : SpecificBaseline
{ -- blah -- }
class SubSub1 : AnotherSubClass
{ -- blah -- }
Here is the simple PerPiece instance that is detecting which TYPE (per above class/subclasses) it should represent / operate like.
class OnePiece
{
public:
ReadSpecificType(string *s);
protected:
[AnyDerivedFromSpecificBaseline] specificType;
}
OnePiece::ReadSpecificType(string *s)
{
if string contains (whatever)
specificType = new WhichSubClassDerivedFromSpecificBaseline();
else if another expected key
specificType = new DifferentSubClassDerivedFromSpecificBaseline();
else
etc...
}
I was even considering generics such as
<typename T>
class MyClass<T>
{
blah...
}
and building an array of pointers for each type, but not sure how to properly create the pointer when it could be of any type of the desired "SpecificBaseline".
Any help, or even alternative solutions appreciated.
Upvotes: 1
Views: 182
Reputation: 70939
What you need is a factory method ReadSpecificType
. Make all the specific types inherit from a common base class and create a factory method that takes as argument the string to be parsed and returns a point to the common base class.
Upvotes: 2