home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1957 < prev    next >
Encoding:
Text File  |  1993-01-03  |  2.5 KB  |  110 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Variants
  5. Message-ID: <1993Jan3.044702.24443@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. Date: Sun, 3 Jan 1993 04:47:02 GMT
  10. Lines: 98
  11.  
  12. Variants, Part II.
  13. ------------------
  14.  
  15. The 'type' clauses of a select statement specify an implict
  16. variant:
  17.  
  18.     variant Num [float, complex, int] x;
  19.  
  20.     select(x)
  21.     {
  22.         type(double d) { ... }
  23.         type(long l)   { ... }
  24.         type(complex c) { ...}
  25.     }
  26.  
  27. In this case the implicit variant is
  28.  
  29.     variant Implicit [double,long,complex];
  30.  
  31. This clearly indicates how variants interconvert,
  32.  
  33.     variant X [X1, X2] x;
  34.     variant Y [Y1, Y2] y;
  35.  
  36.     Y y=x;
  37.  
  38. The conversion is well defined if each of X1 and X2 unambiguously
  39. converts to either Y1 or Y2, where 'unambiguously converts' is
  40. used in the same sense as the normal overload matching rules.
  41.  
  42. The select statement can have multiple arguments:
  43.  
  44.     variant V [V1, V2] v;
  45.     variant W [W1, W2] w;
  46.  
  47.     select(v,w)
  48.     {
  49.         type(V1 *v1, W1 *w1) { ... }
  50.         type(V2 *v2, W1 *w1) { ... }
  51.         type(V1 *v1, W2 *w2) { ... }
  52.         type(V2 *v2, W2 *w2) { ... }
  53.     }
  54.  
  55. The select statement can have a 'default' (none-of-the-above)
  56.  
  57.     select(v)
  58.     {
  59.         type(V1 v1) { ... }
  60.         type(...) { cout << "Type Not Handled"; }
  61.     }
  62.  
  63. Where variants share a common base, it would be more usual
  64. to do:
  65.  
  66.     class B;
  67.     class D1 : B;
  68.     class D2 : B;
  69.  
  70.     variant D [B,D1,D2] *d;
  71.  
  72.     select(d)
  73.     {
  74.         type(D1 *d1) { ...}  // special case
  75.         type(B *b)   { ... } // default to base class
  76.     }
  77.  
  78. Where dynamic downcasting is required, it can be constrained
  79. to a one off function:
  80.  
  81.     D* downcast(B* b)
  82.     {
  83.         D *d;
  84.         if (d=dynamic_cast<D1*>b);
  85.         else if (d=dynamic_cast<D2*>b);
  86.         else d=b;
  87.         return d;
  88.     }
  89.  
  90. allowing the downcast to be performed exactly once in one lexically
  91. enclosed place, and thereafter guarranting secure access to
  92. the required exact types.
  93.  
  94. (Note: this example requires variant assignment)
  95.  
  96. (Note 2: If you are a bit worried about the way the * symbol
  97. got shifted about:
  98.  
  99.     variant V [V1, V2] *vp;
  100.     variant pV [V1*, V2*] vp; // same as above??
  101.  
  102. show it makes sense and deduce a variant is not a type,
  103. or wait for part III)
  104.  
  105. -- 
  106. ;----------------------------------------------------------------------
  107.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  108.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  109. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  110.