Hello, maybe somebody can enlighten me? :)
I wasn't really sure how to concisely describe the following scenario
so it was tricky to search for help.
Simplifying the problem...
I have a home which contains people. I also want a SpecialHome which
has to contain a set of SpecialPersons.
class Home {
Person* getPerson(id);
vector<Person*> m_People;
}
class SpecialHome : public CHome {
SpecialPerson* getPerson(id);
vector<SpecialPerson*> m_People;
}
So obviously the above SpecialHome class isn't how I would want to
implement this. There's nothing being inherited, the list of pointers
are unnecessarily duplicated, etc. The problem is that a number of
Home methods accept and return Persons. In my more complicated
situation, I really want to avoid a templatized Home class (it would
make for quite a confusing class diagram). I guess accepting Persons
isn't such a problem thanks to polymorphism, but returning them is
(I've also got a Person iterator class in an effort to abstract away
the containment mechanism in Home).
The approach I've tried taking is to remove all of these Person-related
methods from Home and put them is a sort of templatized "manipulator"
class. This manipulator was made a friend of Home and SpecialHome so
it more or less can do whatever it pleases. Naturally, there's a lot
of dynamic_casting since the vector lives in the base class (for some
reason dynamic_casting always makes me uncomfortable).
I'm having trouble getting this to compile because of complications
with the more convoluted scenario I'm dealing with.
This lead me to take a step back and ask myself if there isn't a better
way to do design the class structure. I would imagine the scenario I
described above is a fairly common one.
Any tips are greatly appreciated!
cheers!