home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: boolean (was: Re: typedef vs enum)
- Message-ID: <1992Jul23.140854.29546@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <DAVEG.92Jul20220553@synaptx.synaptics.com> <1992Jul21.170922.26941@ucc.su.OZ.AU> <9220415.18737@mulga.cs.mu.OZ.AU>
- Date: Thu, 23 Jul 1992 14:08:54 GMT
- Lines: 70
-
- In article <9220415.18737@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
- >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.
- >
-
- Thanks to those who suggested variants of how to do this
- with the existing language, including
-
- enum ZEROTYPE {ZERO};
-
- which wins as the shortest!
-
- >We ALREADY have a type with only one value -- "void".
-
- Ohh, I thought void had NO values!
-
- {false,true} == boolean
- {false} == ZERO
- {} == void
-
- >
- >If you want a constructor which could only take one possible value value, you
- >should use the void type.
-
- Actually, I want to say
-
- T t;
- t=T();
-
- more nicely.
-
- >There *is* a problem in that C++ does not allow named
- >constructors.
-
- Nasty problem too. And the auto-conversion is even nastier:
-
- class array { public:
- array(int) { ... } // array of n objects
- };
-
- array a;
-
- a=10; // woops, didnt mean that!
-
- >, and so you have to use the default constructor. One workaround is
- >the following:
- >
- > class Zero {
- > public: operator int() const { return 0; }
- > } zero;
- >
-
- In fact, one might have instead
-
- class ZZZZ {} zzzz;
-
- since the class is used for overloading, and we dont need, and mightnt
- want, to be compatible with arithmetic types.
-
- Just shows you what you CAN do in C++ with a bit of imagination :-)
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-