Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!barmar From: barmar@think.com (Barry Margolin) Subject: Re: Better Encapsulation:Overload Keywords Message-ID: <1991Jun24.163530.14631@Think.COM> Sender: news@Think.COM Reply-To: barmar@think.com Organization: Thinking Machines Corporation, Cambridge MA, USA References: <1991Jun23.223737.31644@kuhub.cc.ukans.edu> Date: Mon, 24 Jun 91 16:35:30 GMT Lines: 51 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. The "for" statement is a bit more complex if you want the overloading to be able to take over the initialization and stepping as well. You'd probably need an alternative syntax; maybe: for ( = ) where should evaluate to a class object. The interface would probably be similar to iterators in CLU. I'm not sure how you could combine traditional "for" loops with the overloaded kind in one statement. I'm not sure what an overloaded "switch" statement would be like. Would the labels still be integer constants? If so, then a conversion from the class to int would be sufficient. Of course, all these suggestions about using conversions only work if the same conversion is useful in all the contexts. If you want the test in a "while" statement to be true in a different case from the test in an "if" statement, it won't work. However, I think that would be a very bad idea. The equivalence between while () and