原帖由 c/unix 于 2008-7-6 03:15 發(fā)表
Hi,I don't think your en is some,you know authentic...maybe disgusting
So do not show it anymore. You can make it ,right?
#include <stdio.h> /* 通常情況下程序員應該記得哪個變量是什么類型。但是在有些特殊情況下運行時類型信息獲取是有必要的。 比如要用通用函數(shù)做數(shù)據(jù)的打包和解包,函數(shù)接受基類型的指針作參數(shù)卻要完成對派生類型的打包和解包,這時 就需要運行時類型信息獲取。雖然 C 不直接支持,但是我們可以手工在 “襪子” 上貼個標簽,以便隨時追蹤它。 :-) */ typedef struct { char class_name[2]; int class_size; } Base; typedef struct { Base parent; int ma; } A; typedef struct { Base parent; int mb; char text[20]; } B; A_init(void *this) { Base *pp; pp = (Base*)this; pp->class_name[0] = 'A'; pp->class_name[1] = '\0'; pp->class_size = sizeof(A); } B_init(void *this) { Base *pp; pp = (Base*)this; pp->class_name[0] = 'B'; pp->class_name[1] = '\0'; pp->class_size = sizeof(B); } int main (int argc, char *argv[]) { A a, *pa; B b, *pb; Base *pbase; pa = &a; pb = &b; A_init((void*)pa); B_init((void*)pb); pbase = (Base*)pa; printf("%s size = %d ", pbase->class_name, pbase->class_size); pbase = (Base*)pb; printf("%s size = %d \n", pbase->class_name, pbase->class_size); printf("A size = %d B size = %d \n", sizeof(a), sizeof(b)); return 0; } |
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |