home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11408 < prev    next >
Encoding:
Text File  |  1992-07-23  |  2.2 KB  |  83 lines

  1. Newsgroups: comp.lang.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: Re: boolean (was: Re: typedef vs enum)
  5. Message-ID: <1992Jul23.140854.29546@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. References: <DAVEG.92Jul20220553@synaptx.synaptics.com> <1992Jul21.170922.26941@ucc.su.OZ.AU> <9220415.18737@mulga.cs.mu.OZ.AU>
  10. Date: Thu, 23 Jul 1992 14:08:54 GMT
  11. Lines: 70
  12.  
  13. In article <9220415.18737@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  14. >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. >
  16. >>    While we're talking of a boolean type (which has two
  17. >>values equivalent to 0 and 1), how about a ZERO type?
  18. >>
  19. >>    Type ZERO only has ONE value---0 of course.
  20. >
  21.  
  22.     Thanks to those who suggested variants of how to do this
  23. with the existing language, including
  24.  
  25.     enum ZEROTYPE {ZERO};
  26.  
  27. which wins as the shortest!
  28.  
  29. >We ALREADY have a type with only one value -- "void".
  30.  
  31.     Ohh, I thought void had NO values!
  32.  
  33.     {false,true} == boolean
  34.     {false}      == ZERO
  35.     {}           == void
  36.  
  37. >
  38. >If you want a constructor which could only take one possible value value, you
  39. >should use the void type. 
  40.  
  41.     Actually, I want to say
  42.  
  43.     T t;
  44.     t=T();
  45.  
  46. more nicely.
  47.  
  48. >There *is* a problem in that C++ does not allow named
  49. >constructors.
  50.  
  51.     Nasty problem too. And the auto-conversion is even nastier:
  52.  
  53.     class array { public:
  54.         array(int) { ... } // array of n objects
  55.     };
  56.  
  57.     array a;
  58.  
  59.     a=10;    // woops, didnt mean that!
  60.  
  61. >, and so you have to use the default constructor. One workaround is
  62. >the following:
  63. >
  64. >    class Zero {
  65. >       public: operator int() const { return 0; }
  66. >    } zero;
  67. >
  68.  
  69. In fact, one might have instead
  70.  
  71.     class ZZZZ {} zzzz;
  72.  
  73. since the class is used for overloading, and we dont need, and mightnt
  74. want, to be compatible with arithmetic types.
  75.  
  76. Just shows you what you CAN do in C++ with a bit of imagination :-)
  77.  
  78. -- 
  79. ;----------------------------------------------------------------------
  80.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  81.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  82. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  83.