enum可以做索引, 配上虚函数,或者函数指针,可以实现上层的统一封装和快速索引。
点击(此处)折叠或打开
- MoTbl.cpp
- #include <stdio.h>
- #include <stdlib.h>
- enum {
- eA=0,
- eB,
- eC,
- eD,
- eMax
- };
- class Bs{
- public:
- virtual void say(){ printf("Bs\n"); }
- };
- class A: public Bs{
- int aa[1];
- public:
- void say(){printf("A\n");}
- void call(){printf("A::call\n");}
- };
- class B: public Bs{
- int aa[3];
- public:
- void say(){printf("B\n");}
- void sing(){printf("B::sing\n");}
- };
- class C: public Bs{
- int aa[10];
- public:
- void say(){printf("C\n");}
- };
- class D: public Bs{
- int aa[100];
- public:
- void say(){printf("D\n");}
- };
- class MoTbl{
- public:
- union{
- struct{
- A *a;
- B *b;
- C *c;
- D *d;
- }t;
- void *tv[eMax];
- };
- };
- int
- main ( int argc, char *argv[] )
- {
- int i = 0;
- MoTbl mt;
- A a;
- B b;
- C c;
- D d;
- printf("A:%d\n",sizeof(MoTbl));
- for(i=0; i<eMax; i++)
- printf("%p\n",mt.tv[i]);
- printf("--------------\n");
- mt.tv[eA] = &a;
- mt.tv[eB] = &b;
- mt.tv[eC] = &c;
- mt.tv[eD] = &d;
- for(i=0; i<eMax; i++)
- printf("%p\n",mt.tv[i]);
- printf("--------------\n");
- mt.t.a->say();
- printf("--------------\n");
- for(i=0; i<eMax; i++){
- switch(i){enum 做索引 2012-10-11 18:17:14
分类: C/C++
enum可以做索引, 配上虚函数,或者函数指针,可以实现上层的统一封装和快速索引。
点击(此处)折叠或打开
- MoTbl.cpp
- #include <stdio.h>
- #include <stdlib.h>
- enum {
- eA=0,
- eB,
- eC,
- eD,
- eMax
- };
- class Bs{
- public:
- virtual void say(){ printf("Bs\n"); }
- };
- class A: public Bs{
- int aa[1];
- public:
- void say(){printf("A\n");}
- void call(){printf("A::call\n");}
- };
- class B: public Bs{
- int aa[3];
- public:
- void say(){printf("B\n");}
- void sing(){printf("B::sing\n");}
- };
- class C: public Bs{
- int aa[10];
- public:
- void say(){printf("C\n");}
- };
- class D: public Bs{
- int aa[100];
- public:
- void say(){printf("D\n");}
- };
- class MoTbl{
- public:
- union{
- struct{
- A *a;
- B *b;
- C *c;
- D *d;
- }t;
- void *tv[eMax];
- };
- };
- int
- main ( int argc, char *argv[] )
- {
- int i = 0;
- MoTbl mt;
- A a;
- B b;
- C c;
- D d;
- printf("A:%d\n",sizeof(MoTbl));
- for(i=0; i<eMax; i++)
- printf("%p\n",mt.tv[i]);
- printf("--------------\n");
- mt.tv[eA] = &a;
- mt.tv[eB] = &b;
- mt.tv[eC] = &c;
- mt.tv[eD] = &d;
- for(i=0; i<eMax; i++)
- printf("%p\n",mt.tv[i]);
- printf("--------------\n");
- mt.t.a->say();
- printf("--------------\n");
- for(i=0; i<eMax; i++){
- switch(i){
- case eA: mt.t.a->say(); break;
- case eB: mt.t.b->say(); break;
- case eC: mt.t.c->say(); break;
- case eD: mt.t.d->say(); break;
- }
- }
- printf("--------------\n");
- for(i=0; i<eMax; i++)
- ((Bs *)mt.tv[i])->say();
- printf("--------------\n");
- for(i=0; i<eMax; i++){
- switch(i){
- case eA: mt.t.a->call(); break;
- case eB: mt.t.b->sing(); break;
- }
- }
- printf("--------------\n");
- }
输出
点击(此处)折叠或打开
- A:16
- 0xb7726ff4
- 0xb77238e4
- (nil)
- 0xb7615ff4
- --------------
- 0xbff0cd98
- 0xbff0cd88
- 0xbff0cd5c
- 0xbff0cbc8
- --------------
- A
- --------------
- A
- B
- C
- D
- --------------
- A
- B
- C
- D
- --------------
- A::call
- B::sing
- --------------
- case eA: mt.t.a->say(); break;
- case eB: mt.t.b->say(); break;
- case eC: mt.t.c->say(); break;
- case eD: mt.t.d->say(); break;
- }
- }
- printf("--------------\n");
- for(i=0; i<eMax; i++)
- ((Bs *)mt.tv[i])->say();
- printf("--------------\n");
- for(i=0; i<eMax; i++){
- switch(i){
- case eA: mt.t.a->call(); break;
- case eB: mt.t.b->sing(); break;
- }
- }
- printf("--------------\n");
- }
输出
点击(此处)折叠或打开
- A:16
- 0xb7726ff4
- 0xb77238e4
- (nil)
- 0xb7615ff4
- --------------
- 0xbff0cd98
- 0xbff0cd88
- 0xbff0cd5c
- 0xbff0cbc8
- --------------
- A
- --------------
- A
- B
- C
- D
- --------------
- A
- B
- C
- D
- --------------
- A::call
- B::sing
- --------------
时间: 2024-10-29 06:09:20