Hallo,
ich suche eine Möglichkeit, den folgenden Code durch eine
Arraykonstruktion zu ersetzen:
1 | class x
|
2 | {
|
3 | };
|
4 |
|
5 | class x0 : public x
|
6 | {
|
7 | };
|
8 |
|
9 | class x1 : public x
|
10 | {
|
11 | };
|
12 |
|
13 | class x2 : public x
|
14 | {
|
15 | };
|
16 |
|
17 | enum
|
18 | {
|
19 | TypX0 = 0,
|
20 | TypX1,
|
21 | TypX2,
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 | int main(void)
|
27 | {
|
28 | x *pX;
|
29 | int myTyp = 0;
|
30 |
|
31 | switch (myTyp)
|
32 | {
|
33 | case TypX0:
|
34 | pX = new x0();
|
35 | break;
|
36 |
|
37 | case TypX1:
|
38 | pX = new x1();
|
39 | break;
|
40 |
|
41 | case TypX2:
|
42 | pX = new x2();
|
43 | break;
|
44 | }
|
45 | // do someting
|
46 | }
|
Gibt es in C++ eine Möglichkeit, im obigen Code den switch() Baum durch
einen Arrayzugriff ungefähr so:
1 |
|
2 | x classArray[] = { x0, x1, x2 };
|
3 |
|
4 | pX = new classArray[myTyp];
|
zu ersetzen?
Vielen Dank,
Stefan