I have to write some code, but I don't know, how to do it in the easiest way.
In my program there are:
Class P
Class HP: public P
Class CP: public P
and class M
I have to write M construct with will be able to handle the diffrent combination of input arguments
for example:
HP hp("xxx", "yyy");
HP hp_1("xx1", "yy1");
CP cp("www", "aaa");
CP cp_1("ww1", "aa1");
M m(hp, hp1);
M m_1(hp, cp);
M m_2(cp_1, hp_1);
etc...
Any idea? Do I have write construct for each combination?
Well, as it seems from your question, the classes HP
and CP
have a common base class P
. It totally depends on what M
actually needs by differentiating HP
and CP
. If it's enough for M
to use P
's interface you can probably provide a (single) constructor for M
using
class M {
public:
M(P& a, P& b) {
// Do whatever you didn't specify in your question
}
// Or pointer references if preferred
M(P* a, P* b) {
// Do whatever you didn't specify in your question
}
};
Even if you need to distinguish from HP
and CP
you can still use a dynamic_cast<>
(for both variants mentioned) inside the constructors member initialization list or body.