Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!cs.utexas.edu!uunet!oresoft!dan From: dan@oresoft.uu.net (Daniel Elbaum) Newsgroups: comp.lang.c++ Subject: Re: A question of style Keywords: define const enum type Message-ID: <1989Aug1.193259.6822@oresoft.uu.net> Date: 1 Aug 89 19:32:59 GMT References: <1148@hsfmsh.UUCP> Reply-To: dan@oresoft.uu.net (Daniel Elbaum) Organization: Oregon Software, Portland, OR Lines: 45 In article <1148@hsfmsh.UUCP> mhyman@hsfmsh.UUCP (Marco S. Hyman) writes: :There are three ways I can define literal values of type int in C++. [manifest constants, integral variables of type const, and enumerators of unnamed enumerations] :Using anonymous enums to group related constant values appeals to me because :it adds a touch of documentation -- the implication is that the values are :somehow related. Any problems here? What are the pros and cons of the :three methods? Is there some reason for NOT using a particular method? Hopefully somebody will correct me if I'm wrong. The enum solution creates an unnamed type which is distinct from any other type--in other words, the named integral constants which you'd declare as enumerators are not, strictly speaking, of type int. One ramification of the distinction is that function overloads may be disambiguated on the basis of the expression type, which in the case you describe may actually be a bonus. Another difference is that the enumerators aren't necessarily allowed to take on any legal integer value--just a value which can be promoted to an integer. Your implementation may use shorts, for example. Finally, an enum declaration, like a const declaration, can be used to create local constants: class man_1 { public: enum {poison, meat}; int isfood(int dish) { return(dish == meat); } // dish == 1? }; class man_2 { public: enum {meat, poison}; int isfood(int dish) { return(dish == meat); } // dish == 0? }; -- Spa link snot the temper tent, a few cannery doubt lowed. ({uunet,tektronix,reed,sun!nosun,osu-cis,psu-cs}!oresoft!(dan)@oresoft.uu.net)