Convert to Polymorphism?
Each derived classes are derived from class A through virtual
multiple inheritance. Only class E is defined. Each data members and
member functions are inherited into class E from one base class and
all derived classes.
Each derived classes have the same Run() member function name, but
different implementation. The variable of pFunc_E is a pointer to Run
() member function. It does not have pointer to member function array
because I want to reassign any Run() member function to pFunc_E at run-
time execution.
pFunc_E is like dispatch function. Inheritance is the answer to
divide from one large base class into derived sub-classes (from one
large source code to multiple source code for better readability).
Class E is used to communicate to all sub-classes.
Please tell me if one object with multiple sub-classes good design.
If not, can it be converted to polymorphism? Run() can be set to pure
virtual function in class A and (non-pure) virtual functions to all
derived sub-classes. What is your advice? Please give me your
example if you can. Thanks=85
class E;
class A
{
public:
A();
~A();
void Run_A();
void (E::*pFunc_E)();
void Test();
};
class B1 : virtual public A
{
public:
B1();
~B1();
void Run_B1();
};
class B2 : virtual public A
{
public:
B2();
~B2();
void Run_B2();
};
class B3 : virtual public A
{
public:
B3();
~B3();
void Run_B3();
};
class B4 : virtual public A
{
public:
B4();
~B4();
void Run_B4();
};
class C1 : virtual public B1, virtual public B2, virtual public B3,
virtual public B4
{
public:
C1();
~C1();
void Run_C1();
};
class C2 : virtual public B1, virtual public B2, virtual public B3,
virtual public B4
{
public:
C2();
~C2();
void Run_C2();
};
class C3 : virtual public B1, virtual public B2, virtual public B3,
virtual public B4
{
public:
C3();
~C3();
void Run_C3();
};
class D1 : virtual public C1, virtual public C2, virtual public C3
{
public:
D1();
~D1();
void Run_D1();
};
class D2 : virtual public C1, virtual public C2, virtual public C3
{
public:
D2();
~D2();
void Run_D2();
};
class E : virtual public D1, virtual public D2
{
public:
E();
~E();
void Run_E();
};
A::A() {}
A::~A() {}
void A::Run_A() {}
void A::Test() {}
B1::B1() {}
B1::~B1() {}
void B1::Run_B1() { pFunc_E = &B2::Run_B2; }
B2::B2() {}
B2::~B2() {}
void B2::Run_B2() { pFunc_E = &B3::Run_B3; }
B3::B3() {}
B3::~B3() {}
void B3::Run_B3() { pFunc_E = &B4::Run_B4; }
B4::B4() {}
B4::~B4() {}
void B4::Run_B4() { pFunc_E = &C1::Run_C1; }
C1::C1() {}
C1::~C1() {}
void C1::Run_C1() { pFunc_E = &C2::Run_C2; }
C2::C2() {}
C2::~C2() {}
void C2::Run_C2() { pFunc_E = &C3::Run_C3; }
C3::C3() {}
C3::~C3() {}
void C3::Run_C3() { pFunc_E = &D1::Run_D1; }
D1::D1() {}
D1::~D1() {}
void D1::Run_D1() { pFunc_E = &D2::Run_D2; }
D2::D2() {}
D2::~D2() {}
void D2::Run_D2() { pFunc_E = &B1::Run_B1; }
E::E() {}
E::~E() {}
void E::Run_E() {}
int main()
{
E e[3];
e[0].pFunc_E = &B1::Run_B1;
e[1].pFunc_E = &B1::Run_B1;
e[2].pFunc_E = &B1::Run_B1;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 10; x++)
(e[y].*(e[y].pFunc_E))();
}
system("pause");
return 0;
}