Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!mucs!jk From: jk@cs.man.ac.uk (John Kewley ICL) Newsgroups: comp.lang.c++ Subject: Re: Boolean confusion (class implementation) Message-ID: <2634@m1.cs.man.ac.uk> Date: 5 Jun 91 16:56:25 GMT References: <1991May30.060200.6590@lth.se> <998@elan.Elan.COM> <1991Jun1.214309.15094@lth.se> Sender: news@cs.man.ac.uk Reply-To: jk@cs.man.ac.uk (John Kewley ICL) Distribution: comp.lang.c++ Organization: Department of Computer Science, University of Manchester UK Lines: 178 Here is an example of a boolean class which does not allow ints to be assigned to booleans. I introduce functions which return true and false values. These could be extended for three valued logic. They are friends which use a static (own) variable defined using the *private* int conversion constructor. To offset the inefficiencies of the system (possibly not much worse than any other I've seen suggested) would be to have #ifndef NDEBUG #define TRUE 1 #define FALSE 0 #define bool int /* or perhaps char */ #else #define TRUE true() #define FALSE false() class bool { /* . . . */ } #fi There is no assignment operator for int to bool and no int conversion constructor. Taking the 4 comments made about Tom's class: 1. Boolean takes up 2 bytes No problem if using #defines as above 2. register declarations are not allowed I am not certain of this one. I have used registers for some examples OK, I think (I'm no assembler hacker). register could perhaps (careful slippery ice here) be #defined away when DEBUGging. 3. Assignment by int possible Not that I can see in this case, except in friend functions true() and false(). 4. returning a class is awkward I don't understand this one, this may be avoided by my #defines I think in this case, the good outweighs the bad, in particular there should be no performance penalty when NDEBUGging. Note I have not yet tried to implement the #define stuff, I would welcome peoples ideas. OK here we go here is the class + a sample main file. Sorry about the plethora of operators, I tried to be complete. Have I missed any functionality? ------------------------------------------- #include class bool { public: bool() {} bool(const bool& b) {c=b.c;} // For if statements operator void*() const {return (void*) c;} bool& operator= (const bool& r) {c= r.c; return *this;}; bool operator! () {return (!c) ? true() : false();}; bool operator == (const bool& r) const {return (c == r.c) ? true() : false();}; bool operator != (const bool& r) const {return (c != r.c) ? true() : false();}; bool operator == (int r) const {return (r) ? c : !c;}; bool operator != (int r) const {return (r) ? !c : c;}; friend bool operator == (int l, const bool& r) const {return (l) ? r.c : !r.c;}; friend bool operator != (int l, const bool& r) const {return (l) ? !r.c : r.c;}; bool operator && (const bool& r) const {return (c) ? bool(r.c) : false();}; bool operator || (const bool& r) const {return (c) ? true() : bool(r.c);}; bool operator && (int r) const {return (c) ? bool(r) : false();}; bool operator || (int r) const {return (c) ? true() : bool(r);}; friend bool operator && (int l, const bool& r) const {return (l) ? bool(r.c) : false();}; friend bool operator || (int l, const bool& r) const {return (l) ? true() : bool(r.c);}; friend bool& true(); friend bool& false(); private: char c; bool(int i) {c=((i)?1:0);} // Disallow assignment to an int. // bool& operator= (int) {return *this;}; }; bool& true() { static bool T(1); return T; } bool& false() { static bool F(0); return F; } bool one_more( int a, int b ) { return (( a - 1 == b ) ? true() : false()); } int main() { bool t= true(); bool f= false(); bool b= true(); bool c; if (one_more(2,1)) printf("2 is one more than 1\n"); else printf("2 is not one more than 1\n"); if (one_more(1,2)) printf("1 is one more than 2\n"); else printf("1 is not one more than 2\n"); c= false(); b= c; // b= 1; // bool::bool(): private member // I think it refers to bool::bool(int) (b) ? printf("b is true\n") : printf("b is not true\n"); (!b) ? printf("b is false\n") : printf("b is not false\n"); (b != c) ? printf("b and c are both the same\n") : printf("b and c are not the same\n"); if (b) printf("b = true\n"); else printf("b = false\n"); return(0); if (g(1,f)) then printf("true\n"); else printf("false\n"); } -- J.K. John M. Kewley, ICL, Wenlock Way, West Gorton, Manchester. M12 5DR Tel: (+44) 61 223 1301 X2138 Email: jk@cs.man.ac.uk / jk@nw.stl.stc.co.uk