Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!spool.mu.edu!uunet!sdl.mdcbbs.com!alanb From: alanb@sdl.mdcbbs.com Newsgroups: comp.lang.c++ Subject: Re: Better Encapsulation:Overload Keywords Message-ID: <1991Jun25.103644.1@sdl.mdcbbs.com> Date: 25 Jun 91 09:36:44 GMT Article-I.D.: sdl.1991Jun25.103644.1 References: <1991Jun23.223737.31644@kuhub.cc.ukans.edu> <1991Jun24.163530.14631@Think.COM> Organization: Shape Data Ltd. (McDonnell Douglas M&E, Cambridge UK) Lines: 48 Nntp-Posting-Host: shapeg Nntp-Posting-User: alanb In article <1991Jun24.163530.14631@Think.COM>, barmar@think.com (Barry Margolin) writes: > In article <1991Jun23.223737.31644@kuhub.cc.ukans.edu> blythe@kuhub.cc.ukans.edu writes: >> One of the best things about C++ is its ability to overload an >>operator for a certain combination of objects. These operators are then >>implemented as a function by the compiler. Why not keywords also? In >>specific, the selection keywords (if...else and switch) and the iteration >>keywords (while, do, and for) are the only keywords I see having good >>potential when overloaded. > > If C++ had a real boolean type, and required the test expression of an > "if", "while", "do", and "for" statements to be of this type (perhaps with > a built-in conversion from integral and pointer types, for > back-compatibility) then much of this could be done by defining a > conversion from the class to boolean. > [Discussion of "for" omitted. CLU style iterators would be nice though.] We have a C preprocessor that provides a boolean type and enforces the rule that test expressions must be booleans (e.g. if (pointer != NULL), not if (pointer)). (Now we use C++, we just have a coding convention). If you allow conversion from integral and pointer types, you have lost the type enforcement you might want, so why not just define operator (void *) for the classes you want to use in while loops? Something like { if (my_status) return this; else return NULL ); if (anobject) { foo(); } is now equivalent to if (anobject.status()) { foo(); } and while uses the same test as if. (this assumes you never want to coerce to a pointer other than when your if/while condition is true - in many cases this is reasonable.) (The preprocessor also provided function prototypes before ANSI C was around, and workarounds for assorted platform compiler bugs - I'm not claiming that just having a boolean type is necessarily worth the effort). > Barry Margolin, Thinking Machines Corp. > > barmar@think.com > {uunet,harvard}!think!barmar Alan Braggins alanb@sdl.mdcbbs.com "Any resemblance between these opinions,..."