Path: utzoo!attcan!uunet!seismo!dimacs.rutgers.edu!mips!apple!voder!procase!roger From: roger@procase.UUCP (Roger H. Scott) Newsgroups: comp.lang.c++ Subject: Re: #defines.... Message-ID: <172@logo.procase.UUCP> Date: 11 Jul 90 01:14:28 GMT References: <37786@genrad.UUCP> Reply-To: roger@procase.UUCP (Roger H. Scott) Organization: proCASE Corporation, Santa Clara, CA Lines: 34 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... > Well, if C++ had types and functions as first-class-objects in the language your statement might have a chance of being true, but since C++ doesn't your statement doesn't. As a simple example, how would you write achieve the effect of a SWAP macro in C++? #define SWAP(T,a,b) {T t = a; a = b; b = t;} Until parameterized types come along (and they do the right thing), another example is generation of declarations of parameterized types. Another example is aliasing of identifier names in header files that you can't change, e.g.: read_only.h: class Foo { void dont_you_hate_this_lexical_style(); }; clean_it_up.c: #include "read_only.h" #define differentLexicalStyle dont_you_hate_this_lexical_style ... p->differentLexicalStyle(); This list is not exhaustive.