home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1991 / 02 / grdlagen / virtual.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  2.3 KB  |  94 lines

  1. //----------------------------------------------------------
  2. //                      VIRTUAL.C                          -
  3. //        (c) 1990 by Thole Groeneveld & toolbox           -
  4. //----------------------------------------------------------
  5.  
  6. /*
  7.  Dieses Programm demonstriert den virtuellen Aufruf-
  8.  mechanismus in C++.
  9.  Weitere Informationen siehe Ellis & Stroustrup :
  10.  The Annotated C++ Reference Manual, S.227 ff.
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. typedef struct TabInfo {
  16.  void (*f)();         // Zeiger auf virtuelle Memberfunktion
  17.  int offset;          // Korrekturwert des "this" Zeigers
  18. } TabInfo;            // (nur bei Mehrfachvererbung)
  19.  
  20. // Eine Basisklasse
  21. typedef struct base {
  22.  TabInfo *ti;         // Zeiger auf "Virtual Function Table"
  23.  int base_info;
  24. } base;
  25.  
  26. // class D1 : public base ...
  27. typedef struct {
  28.  base b;
  29.  int D1_info;
  30. } D1;
  31.  
  32. // class D2 : public ... , public base ... (Mult. Inherit.)
  33. typedef struct {
  34.  double D2_info;
  35.  base b;               // base ist nach hinter "verruscht" !
  36. } D2;
  37.  
  38. void print_base (base *this) {
  39.  printf ("base.info = %d \n", this -> base_info);
  40. }
  41.  
  42. void print_D1 (D1 *this) {
  43.  printf ("base.info = %d \n", this -> b.base_info);
  44.  printf ("  D1.info = %d \n", this -> D1_info);
  45. }
  46.  
  47. void print_D2 (D2 *this) {
  48.  printf ("base.info = %d \n", this -> b.base_info);
  49.  printf ("  D2.info = %lf \n", this -> D2_info);
  50. }
  51.  
  52. void f_base (base *this) {
  53.  puts ("Hier ist b1");
  54. }
  55.  
  56. void f_D1 (D1 *this) {
  57.  puts ("Hier ist d1");
  58. }
  59.  
  60. void f_D2 (D2 *this) {
  61.  puts ("Hier ist d2");
  62. }
  63.  
  64. // Virtual Function Tables
  65. TabInfo base_ti_arr[] = {{print_base, 0}, { f_base, 0 }};
  66. TabInfo D1_ti_arr[]   = {{print_D1,   0}, { f_D1,   0 }};
  67. TabInfo D2_ti_arr[]   = {{print_D2, -sizeof(double)},
  68. {f_D2,     -sizeof(double)}};
  69.  
  70. // Initialisierung der "Klassen"
  71. base b1 =   { base_ti_arr, 100 };
  72. D1 d1   = { { D1_ti_arr,   101 }, 123 };
  73. D2 d2   =   { 321.0, { D2_ti_arr, 102 } };
  74.  
  75. // Für print sind alle übergebenen Zeiger von Typ base*
  76. void print (base *b) {
  77.  b -> ti[1].f ((char*) b + (b -> ti[1].offset));
  78.  b -> ti[0].f ((char*) b + (b -> ti[0].offset));
  79. }
  80.  
  81. void main()
  82. {
  83.  base *b;
  84.  
  85.  b = &b1;
  86.  print(b);
  87.  b = &d1.b;
  88.  print(b);
  89.  b = &d2.b;
  90.  print(b);
  91. }
  92. //----------------------------------------------------------
  93. //                  Ende von VIRTUAL.C                     -
  94.