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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: boolean (was: Re: typedef vs enum)
  5. Message-ID: <9220415.18737@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <DOUGM.92Jul19222728@titan.cs.rice.edu> <DAVEG.92Jul20220553@synaptx.synaptics.com> <1992Jul21.170922.26941@ucc.su.OZ.AU>
  9. Date: Wed, 22 Jul 1992 05:17:53 GMT
  10. Lines: 47
  11.  
  12. maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  13.  
  14. >    While we're talking of a boolean type (which has two
  15. >values equivalent to 0 and 1), how about a ZERO type?
  16. >
  17. >    Type ZERO only has ONE value---0 of course.
  18.  
  19. We ALREADY have a type with only one value -- "void".
  20.  
  21. >    What use is it, you ask?
  22. >
  23. >    Plenty. 0 is very important.
  24. >
  25. >    How often have you wanted to allow assignment
  26. >or initialisation of a class to 0 (but not any other value?)
  27. >
  28. >    For example, if p is a pointer, you can write
  29. >
  30. >    p=0; // null pointer
  31. >
  32. >But if it is a *smart* pointer, you would have to declare
  33. >assignment of say int->smart, and give a run-time error
  34. >if the int was non-zero.
  35.  
  36. If you want a constructor which could only take one possible value value, you
  37. should use the void type. There *is* a problem in that C++ does not allow named
  38. constructors, and so you have to use the default constructor. One workaround is
  39. the following:
  40.  
  41.     class Zero {
  42.        public: operator int() const { return 0; }
  43.     } zero;
  44.  
  45.     class smart {
  46.        private: void *p;
  47.        public: smart(Zero z) : p(z) {}
  48.        ...
  49.     };
  50.  
  51. Then you can just use smart(zero) instead of smart(0).
  52. [Alternatively you can just use class Zero {}, but you tend to get spurious
  53. warnings from the compiler because variables of class Zero are never used]
  54. --
  55. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  56. This .signature VIRUS is a self-referential statement that is true - but 
  57. you will only be able to consistently believe it if you copy it to your own
  58. .signature file!
  59.