home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: boolean (was: Re: typedef vs enum)
- Message-ID: <9220415.18737@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <DOUGM.92Jul19222728@titan.cs.rice.edu> <DAVEG.92Jul20220553@synaptx.synaptics.com> <1992Jul21.170922.26941@ucc.su.OZ.AU>
- Date: Wed, 22 Jul 1992 05:17:53 GMT
- Lines: 47
-
- maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
-
- > While we're talking of a boolean type (which has two
- >values equivalent to 0 and 1), how about a ZERO type?
- >
- > Type ZERO only has ONE value---0 of course.
-
- We ALREADY have a type with only one value -- "void".
-
- > What use is it, you ask?
- >
- > Plenty. 0 is very important.
- >
- > How often have you wanted to allow assignment
- >or initialisation of a class to 0 (but not any other value?)
- >
- > For example, if p is a pointer, you can write
- >
- > p=0; // null pointer
- >
- >But if it is a *smart* pointer, you would have to declare
- >assignment of say int->smart, and give a run-time error
- >if the int was non-zero.
-
- If you want a constructor which could only take one possible value value, you
- should use the void type. There *is* a problem in that C++ does not allow named
- constructors, and so you have to use the default constructor. One workaround is
- the following:
-
- class Zero {
- public: operator int() const { return 0; }
- } zero;
-
- class smart {
- private: void *p;
- public: smart(Zero z) : p(z) {}
- ...
- };
-
- Then you can just use smart(zero) instead of smart(0).
- [Alternatively you can just use class Zero {}, but you tend to get spurious
- warnings from the compiler because variables of class Zero are never used]
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature VIRUS is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-