Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!ucsd!pacbell.com!ames!sparkyfs!mckenney From: mckenney@sparkyfs.istc.sri.com (Paul Mckenney) Newsgroups: comp.lang.c++ Subject: Re: #defines.... Message-ID: <32395@sparkyfs.istc.sri.com> Date: 4 Jul 90 20:27:56 GMT References: <37786@genrad.UUCP> Reply-To: mckenney@itstd.sri.com (Paul E. McKenney) Organization: SRI International, Menlo Park, CA 94025 Lines: 25 In article <37786@genrad.UUCP> slp@genrad.genrad.COM (Steven L. Peters) writes: >Just out of curiousity... >With the C++ const, enum, and inline declarations, is it ever >necessary to use #define in a C++ program? Stroustrup emphatically >says in _The C++ Programming Language_ not to use them if you don't >have to. I'm wondering if you ever have to. I can't think of a >single time where you would be forced to use a #define over const or >inline... Until templates are commonly implemented, I will use macros in their stead. I also have used them in cases where I would use a nested function, if such a beast were available in C++. Macros have definite drawbacks, but so do functions with tons of parameters. The good news is that C++ classes can be used (abused?) to reduce the need for nested functions (as compared to C). The trick is to replace the top-level function with a class, with the class's instance variables serving in place of the function's local variables. The code that was in the function and in each local macro is then placed into a (possibly inlined) member function. There are still occasions where use of macros is cleaner, particularly if macro concatenation and stringification facilities come into play. Perhaps nested classes will do even better. Thanx, Paul