Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!kuhub.cc.ukans.edu!blythe From: blythe@kuhub.cc.ukans.edu Newsgroups: comp.lang.c++ Subject: Better Encapsulation:Overload Keywords Message-ID: <1991Jun23.223737.31644@kuhub.cc.ukans.edu> Date: 23 Jun 91 22:37:37 CDT Organization: University of Kansas Academic Computing Services Lines: 89 In C++'s effort to encapsulate data and methods, it has missed some points. A Proposal: Overloading Keywords 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. Take the following normal code as my example: class An_Object { int ready; ... public: int status(void); ... }; int An_Object::status(void) { return(ready); } int main(void) { An_Object object; ... while(object.status()) { ... } ... } The code deciding the termination of the loop is in the object, which is good encapsulation. I was once told that the best objects will know everything you plan to do with them, implemented through member functions (methods). Notice though that the while statement is directly interacting with the object; the object doesn't know how to execute a loop so the while is obviously needed. What I am saying is that the object should know how to interact with the while, which is even better encapsulation than the above example. Take the following code for example: class An_Object { int ready; ... public: An_Object keyword while; ... }; An_Object::keyword while { ready; //statement to be evaluated by a while loop } int main(void) { An_Object object; ... while(object) //better encapsulation { ... } ... } Every time the while loop is executed the statement defined inside the object is evaluated, it's that simple for the while. Overloading other keywords may prove to be a little more challenging, but not much. Overloaded operators are implemented as functions by C++ compilers. Overloaded keywords could be implemented as functions placed inside the real keywords or the statements could simply be placed in the correct place by a preprocessor. Overloaded operators let us get away from code like this: result = add(complex_number_1, complex_number_2); to simply: result = complex_number_1 + complex_number_2; Overloaded keywords could let us get away from code like this: while(object.status()) {...} to simply: while(object) {...} Thank you for your time. Any comments are welcome. Garrett Arch Blythe.