Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 17 Oct 1996 11:59:30 -0600 (MDT)
From:      Nate Williams <nate@mt.sri.com>
To:        "Marty Leisner" <leisner@sdsp.mc.xerox.com>
Cc:        phk@freebsd.org, hackers@freebsd.org
Subject:   Re: enum considered bad ? 
Message-ID:  <199610171759.LAA19045@rocky.mt.sri.com>
In-Reply-To: <9610171708.AA28362@gnu.sdsp.mc.xerox.com>
References:  <2022.845535270@critter.tfs.com> <9610171708.AA28362@gnu.sdsp.mc.xerox.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <stdio.h>
#include <iostream.h>

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;
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199610171759.LAA19045>