From owner-freebsd-hackers Thu Oct 17 11:03:27 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id LAA17242 for hackers-outgoing; Thu, 17 Oct 1996 11:03:27 -0700 (PDT) Received: from rocky.mt.sri.com (rocky.mt.sri.com [206.127.76.100]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id LAA17220; Thu, 17 Oct 1996 11:03:22 -0700 (PDT) Received: (from nate@localhost) by rocky.mt.sri.com (8.7.5/8.7.3) id LAA19045; Thu, 17 Oct 1996 11:59:30 -0600 (MDT) Date: Thu, 17 Oct 1996 11:59:30 -0600 (MDT) Message-Id: <199610171759.LAA19045@rocky.mt.sri.com> From: Nate Williams To: "Marty Leisner" Cc: phk@freebsd.org, hackers@freebsd.org Subject: Re: enum considered bad ? In-Reply-To: <9610171708.AA28362@gnu.sdsp.mc.xerox.com> References: <2022.845535270@critter.tfs.com> <9610171708.AA28362@gnu.sdsp.mc.xerox.com> Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk Marty Leisner writes: > > > > I've noticed that "enum" is hardly ever used in C programs, is this > > because people consider it a bad idea or because they havn't really > > got the swing of it ? > > You mean instead of #define...? > > Its an excellent idea...I saw it in a Doug McIlroy Computing Systems > article, and he explained it to me. > > I never saw it documented any (why to use enum's for constants). In C++ it's really the only way to have 'static' constants that don't have to be initialized inside of class definitions, which you can do also do comparison's against and have type-checking. Nate ---------- cut here ------------ /* Quick and dirty. Forgive the obnoxious mix of C/C++ libraries */ #include #include class foo { public: foo(const char *); void print(void); private: enum { MIN_LEN = 0, MAX_LEN = 500 }; char buff[MAX_LEN]; }; foo::foo(const char *str) { // Null terminate buff[0] = '\0'; if ( ::strlen(str) <= MAX_LEN ) ::strcpy(buff, str); return; } void foo::print(void) { cout << buff << "\n"; } int main() { foo A("Hello World"); A.print(); return 1; }